<!DOCTYPE HTML>
<html lang="zh-CN">


<head>
    <meta charset="utf-8">
    <meta name="keywords" content="第4章-标签亲和性污点容忍度, 博客，个人经验分离，kubernetes,k8s,Linux运维，Python">
    <meta name="description" content="一、标签1.1 什么是标签标签其实就一对key&amp;#x2F;value，被关联到对象上，比如Pod,标签的使用我们倾向于能够表示对象的特殊特点，就是一眼就看出了这个Pod是干什么的，标签可以用来划分特定的对象（比如版本，服务类型等），标签可以">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=no">
    <meta name="renderer" content="webkit|ie-stand|ie-comp">
    <meta name="mobile-web-app-capable" content="yes">
    <meta name="format-detection" content="telephone=no">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="apple-mobile-web-app-status-bar-style" content="black-translucent">
    <meta name="referrer" content="no-referrer-when-downgrade">
    <!-- Global site tag (gtag.js) - Google Analytics -->


    <title>第4章-标签亲和性污点容忍度 | 躺平的人生</title>
    <link rel="icon" type="image/png" href="/favicon.png">
    


    <!-- bg-cover style     -->



<link rel="stylesheet" type="text/css" href="/libs/awesome/css/all.min.css">
<link rel="stylesheet" type="text/css" href="/libs/materialize/materialize.min.css">
<link rel="stylesheet" type="text/css" href="/libs/aos/aos.css">
<link rel="stylesheet" type="text/css" href="/libs/animate/animate.min.css">
<link rel="stylesheet" type="text/css" href="/libs/lightGallery/css/lightgallery.min.css">
<link rel="stylesheet" type="text/css" href="/css/matery.css">
<link rel="stylesheet" type="text/css" href="/css/my.css">
<link rel="stylesheet" type="text/css" href="/css/dark.css" media="none" onload="if(media!='all')media='all'">




    <link rel="stylesheet" href="/libs/tocbot/tocbot.css">
    <link rel="stylesheet" href="/css/post.css">




    
        <link rel="stylesheet" type="text/css" href="/css/reward.css">
    



    <script src="/libs/jquery/jquery-3.6.0.min.js"></script>

<meta name="generator" content="Hexo 6.3.0"></head>


<body>
    <header class="navbar-fixed">
    <nav id="headNav" class="bg-color nav-transparent">
        <div id="navContainer" class="nav-wrapper container">
            <div class="brand-logo">
                <a href="/" class="waves-effect waves-light">
                    
                    <img src="/medias/logo.png" class="logo-img" alt="LOGO">
                    
                    <span class="logo-span">躺平的人生</span>
                </a>
            </div>
            

<a href="#" data-target="mobile-nav" class="sidenav-trigger button-collapse"><i class="fas fa-bars"></i></a>
<ul class="right nav-menu">
  
  <li class="hide-on-med-and-down nav-item">
    
    <a href="/" class="waves-effect waves-light">
      
      <i class="fas fa-home" style="zoom: 0.6;"></i>
      
      <span>首页</span>
    </a>
    
  </li>
  
  <li class="hide-on-med-and-down nav-item">
    
    <a href="/tags" class="waves-effect waves-light">
      
      <i class="fas fa-tags" style="zoom: 0.6;"></i>
      
      <span>标签</span>
    </a>
    
  </li>
  
  <li class="hide-on-med-and-down nav-item">
    
    <a href="/categories" class="waves-effect waves-light">
      
      <i class="fas fa-bookmark" style="zoom: 0.6;"></i>
      
      <span>分类</span>
    </a>
    
  </li>
  
  <li class="hide-on-med-and-down nav-item">
    
    <a href="/archives" class="waves-effect waves-light">
      
      <i class="fas fa-archive" style="zoom: 0.6;"></i>
      
      <span>归档</span>
    </a>
    
  </li>
  
  <li class="hide-on-med-and-down nav-item">
    
    <a href="/about" class="waves-effect waves-light">
      
      <i class="fas fa-user-circle" style="zoom: 0.6;"></i>
      
      <span>关于</span>
    </a>
    
  </li>
  
  <li class="hide-on-med-and-down nav-item">
    
    <a href="/friends" class="waves-effect waves-light">
      
      <i class="fas fa-address-book" style="zoom: 0.6;"></i>
      
      <span>友情链接</span>
    </a>
    
  </li>
  
  <li>
    <a href="#searchModal" class="modal-trigger waves-effect waves-light">
      <i id="searchIcon" class="fas fa-search" title="搜索" style="zoom: 0.85;"></i>
    </a>
  </li>
  <li>
    <a href="javascript:;" class="waves-effect waves-light" onclick="switchNightMode()" title="深色/浅色模式" >
      <i id="sum-moon-icon" class="fas fa-sun" style="zoom: 0.85;"></i>
    </a>
  </li>
</ul>


<div id="mobile-nav" class="side-nav sidenav">

    <div class="mobile-head bg-color">
        
        <img src="/medias/logo.png" class="logo-img circle responsive-img">
        
        <div class="logo-name">躺平的人生</div>
        <div class="logo-desc">
            
            本站记录本人各种学习的旅途，笔记，用于巩固自我并启发后来人
            
        </div>
    </div>

    <ul class="menu-list mobile-menu-list">
        
        <li class="m-nav-item">
	  
		<a href="/" class="waves-effect waves-light">
			
			    <i class="fa-fw fas fa-home"></i>
			
			首页
		</a>
          
        </li>
        
        <li class="m-nav-item">
	  
		<a href="/tags" class="waves-effect waves-light">
			
			    <i class="fa-fw fas fa-tags"></i>
			
			标签
		</a>
          
        </li>
        
        <li class="m-nav-item">
	  
		<a href="/categories" class="waves-effect waves-light">
			
			    <i class="fa-fw fas fa-bookmark"></i>
			
			分类
		</a>
          
        </li>
        
        <li class="m-nav-item">
	  
		<a href="/archives" class="waves-effect waves-light">
			
			    <i class="fa-fw fas fa-archive"></i>
			
			归档
		</a>
          
        </li>
        
        <li class="m-nav-item">
	  
		<a href="/about" class="waves-effect waves-light">
			
			    <i class="fa-fw fas fa-user-circle"></i>
			
			关于
		</a>
          
        </li>
        
        <li class="m-nav-item">
	  
		<a href="/friends" class="waves-effect waves-light">
			
			    <i class="fa-fw fas fa-address-book"></i>
			
			友情链接
		</a>
          
        </li>
        
        
        <li><div class="divider"></div></li>
        <li>
            <a href="https://miaohsukang.gitee.io" class="waves-effect waves-light" target="_blank">
                <i class="fab fa-github-square fa-fw"></i>Fork Me
            </a>
        </li>
        
    </ul>
</div>


        </div>

        
            <style>
    .nav-transparent .github-corner {
        display: none !important;
    }

    .github-corner {
        position: absolute;
        z-index: 10;
        top: 0;
        right: 0;
        border: 0;
        transform: scale(1.1);
    }

    .github-corner svg {
        color: #F062A7;
        fill: #fff;
        height: 64px;
        width: 64px;
    }

    .github-corner:hover .octo-arm {
        animation: a 0.56s ease-in-out;
    }

    .github-corner .octo-arm {
        animation: none;
    }

    @keyframes a {
        0%,
        to {
            transform: rotate(0);
        }
        20%,
        60% {
            transform: rotate(-25deg);
        }
        40%,
        80% {
            transform: rotate(10deg);
        }
    }
</style>

<a href="https://miaohsukang.gitee.io" class="github-corner tooltipped hide-on-med-and-down" target="_blank"
   data-tooltip="Fork Me" data-position="left" data-delay="50">
    <svg viewBox="0 0 250 250" aria-hidden="true">
        <path d="M0,0 L115,115 L130,115 L142,142 L250,250 L250,0 Z"></path>
        <path d="M128.3,109.0 C113.8,99.7 119.0,89.6 119.0,89.6 C122.0,82.7 120.5,78.6 120.5,78.6 C119.2,72.0 123.4,76.3 123.4,76.3 C127.3,80.9 125.5,87.3 125.5,87.3 C122.9,97.6 130.6,101.9 134.4,103.2"
              fill="currentColor" style="transform-origin: 130px 106px;" class="octo-arm"></path>
        <path d="M115.0,115.0 C114.9,115.1 118.7,116.5 119.8,115.4 L133.7,101.6 C136.9,99.2 139.9,98.4 142.2,98.6 C133.8,88.0 127.5,74.4 143.8,58.0 C148.5,53.4 154.0,51.2 159.7,51.0 C160.3,49.4 163.2,43.6 171.4,40.1 C171.4,40.1 176.1,42.5 178.8,56.2 C183.1,58.6 187.2,61.8 190.9,65.4 C194.5,69.0 197.7,73.2 200.1,77.6 C213.8,80.2 216.3,84.9 216.3,84.9 C212.7,93.1 206.9,96.0 205.4,96.6 C205.1,102.4 203.0,107.8 198.3,112.5 C181.9,128.9 168.3,122.5 157.7,114.1 C157.9,116.9 156.7,120.9 152.7,124.9 L141.0,136.5 C139.8,137.7 141.6,141.9 141.8,141.8 Z"
              fill="currentColor" class="octo-body"></path>
    </svg>
</a>
        
    </nav>

</header>

    
<script src="/libs/cryptojs/crypto-js.min.js"></script>
<script>
    (function() {
        let pwd = '';
        if (pwd && pwd.length > 0) {
            if (pwd !== CryptoJS.SHA256(prompt('请输入访问本文章的密码')).toString(CryptoJS.enc.Hex)) {
                alert('密码错误，将返回主页！');
                location.href = '/';
            }
        }
    })();
</script>




<div class="bg-cover pd-header post-cover" style="background-image: url('https://raw.githubusercontent.com/miaohsukang/blog-pic/master/202302142203068.jpg')">
    <div class="container" style="right: 0px;left: 0px;">
        <div class="row">
            <div class="col s12 m12 l12">
                <div class="brand">
                    <h1 class="description center-align post-title">第4章-标签亲和性污点容忍度</h1>
                </div>
            </div>
        </div>
    </div>
</div>




<main class="post-container content">

    
    <div class="row">
    <div id="main-content" class="col s12 m12 l9">
        <!-- 文章内容详情 -->
<div id="artDetail">
    <div class="card">
        <div class="card-content article-info">
            <div class="row tag-cate">
                <div class="col s7">
                    
                    <div class="article-tag">
                        
                            <a href="/tags/k8s%E6%A0%87%E7%AD%BE/">
                                <span class="chip bg-color">k8s标签</span>
                            </a>
                        
                            <a href="/tags/k8s%E4%BA%B2%E5%92%8C%E6%80%A7/">
                                <span class="chip bg-color">k8s亲和性</span>
                            </a>
                        
                    </div>
                    
                </div>
                <div class="col s5 right-align">
                    
                    <div class="post-cate">
                        <i class="fas fa-bookmark fa-fw icon-category"></i>
                        
                            <a href="/categories/K8S%E7%AC%94%E8%AE%B0/" class="post-category">
                                K8S笔记
                            </a>
                        
                    </div>
                    
                </div>
            </div>

            <div class="post-info">
                
                <div class="post-date info-break-policy">
                    <i class="far fa-calendar-minus fa-fw"></i>发布日期:&nbsp;&nbsp;
                    2023-02-14
                </div>
                

                
                <div class="post-date info-break-policy">
                    <i class="far fa-calendar-check fa-fw"></i>更新日期:&nbsp;&nbsp;
                    2023-02-15
                </div>
                

                
                <div class="info-break-policy">
                    <i class="far fa-file-word fa-fw"></i>文章字数:&nbsp;&nbsp;
                    10.3k
                </div>
                

                
                <div class="info-break-policy">
                    <i class="far fa-clock fa-fw"></i>阅读时长:&nbsp;&nbsp;
                    46 分
                </div>
                

                
                    <div id="busuanzi_container_page_pv" class="info-break-policy">
                        <i class="far fa-eye fa-fw"></i>阅读次数:&nbsp;&nbsp;
                        <span id="busuanzi_value_page_pv"></span>
                    </div>
				
            </div>
        </div>
        <hr class="clearfix">

        
        <!-- 是否加载使用自带的 prismjs. -->
        <link rel="stylesheet" href="/libs/prism/prism.min.css">
        

        

        <div class="card-content article-card-content">
            <div id="articleContent">
                <h3 id="一、标签"><a href="#一、标签" class="headerlink" title="一、标签"></a>一、标签</h3><h4 id="1-1-什么是标签"><a href="#1-1-什么是标签" class="headerlink" title="1.1 什么是标签"></a>1.1 什么是标签</h4><p>标签其实就一对key&#x2F;value，被关联到对象上，比如Pod,标签的使用我们倾向于能够表示对象的特殊特点，就是一眼就看出了这个Pod是干什么的，标签可以用来划分特定的对象（比如版本，服务类型等），标签可以在创建一个对象的时候直接定义，也可以在后期随时修改，每一个对象可以拥有多个标签，但是，key值必须是唯一的。创建标签之后也可以方便我们对资源进行分组管理。如果对pod打标签，之后就可以使用标签来查看、删除指定的pod。</p>
<p>在k8s中，大部分资源都可以打标签；标签实际上是从Pod的角度，约束了Pod只能调度到那些满足条件的节点上。</p>
<h4 id="1-2-给pod资源打标签"><a href="#1-2-给pod资源打标签" class="headerlink" title="1.2 给pod资源打标签"></a>1.2 给pod资源打标签</h4><p>标签是打在 Node 上，可以理解为 Node 的属性，或者说你为 Node 分的组名</p>
<pre class="line-numbers language-bash" data-language="bash"><code class="language-bash"><span class="token punctuation">[</span>root@master1 pod<span class="token punctuation">]</span><span class="token comment"># kubectl apply -f pod-first.yaml </span>
pod/tomcat-test-1 created
<span class="token punctuation">[</span>root@master1 pod<span class="token punctuation">]</span><span class="token comment"># kubectl get pods</span>
NAME            READY   STATUS    RESTARTS   AGE
tomcat-test-1   <span class="token number">1</span>/1     Running   <span class="token number">0</span>          32s
<span class="token punctuation">[</span>root@master1 pod<span class="token punctuation">]</span><span class="token comment"># kubectl get pods --show-labels</span>
NAME            READY   STATUS    RESTARTS   AGE   LABELS
tomcat-test-1   <span class="token number">1</span>/1     Running   <span class="token number">0</span>          36s   <span class="token assign-left variable">app</span><span class="token operator">=</span>tomcat<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>

<p>对已经存在的pod打标签</p>
<p>[root@master1~]# <code>kubectl label pods tomcat-test-1  release=v1</code></p>
<p>查看标签是否打成功：</p>
<p>[root@master1~]# <code>kubectl get pods tomcat-test --show-labels</code></p>
<pre class="line-numbers language-bash" data-language="bash"><code class="language-bash"><span class="token punctuation">[</span>root@master1 pod<span class="token punctuation">]</span><span class="token comment"># kubectl get pods tomcat-test-1 --show-labels</span>
NAME            READY   STATUS    RESTARTS   AGE    LABELS
tomcat-test-1   <span class="token number">1</span>/1     Running   <span class="token number">0</span>          2m7s   <span class="token assign-left variable">app</span><span class="token operator">=</span>tomcat,release<span class="token operator">=</span>v1<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span></span></code></pre>

<h4 id="1-3-查看资源标签"><a href="#1-3-查看资源标签" class="headerlink" title="1.3  查看资源标签"></a>1.3  查看资源标签</h4><p>查看默认名称空间下所有pod资源的标签</p>
<p>[root@master1~]# <code>kubectl get pods --show-labels </code></p>
<p>查看默认名称空间下指定pod具有的所有标签</p>
<p>[root@master1~]# <code>kubectl get pods tomcat-test --show-labels</code></p>
<p>列出默认名称空间下标签key是release的pod，不显示标签</p>
<p>[root@master1~]# <code>kubectl get pods -l release</code></p>
<p>列出默认名称空间下标签key是release、值是v1的pod，不显示标签</p>
<p>[root@master1~]# <code>kubectl get pods -l release=v1</code></p>
<p>当标签较多时，也可以使用<code>-L</code> 来指定显示那些标签。列出默认名称空间下标签key是release的所有pod，并打印对应的标签值</p>
<p>[root@master1~]# <code>kubectl get pods -L release</code></p>
<p>除此之外，也使用 in 和 notin 来判断标签的集合关系</p>
<pre class="line-numbers language-bash" data-language="bash"><code class="language-bash"><span class="token punctuation">[</span>root@master01 ~<span class="token punctuation">]</span><span class="token comment"># kubectl get pod -l "app in (web,nginx)" --show-labels</span>
NAME         READY   STATUS    RESTARTS   AGE    LABELS
nginx-test   <span class="token number">1</span>/1     Running   <span class="token number">0</span>          5d8h   <span class="token assign-left variable">app</span><span class="token operator">=</span>nginx,env<span class="token operator">=</span>test
<span class="token punctuation">[</span>root@master01 ~<span class="token punctuation">]</span><span class="token comment">#</span>
<span class="token punctuation">[</span>root@master01 ~<span class="token punctuation">]</span><span class="token comment"># kubectl get pod -l "env notin (product,dev)" --show-labels</span>
NAME                               READY   STATUS    RESTARTS   AGE    LABELS
nginx-test                         <span class="token number">1</span>/1     Running   <span class="token number">0</span>          5d8h   <span class="token assign-left variable">app</span><span class="token operator">=</span>nginx,env<span class="token operator">=</span>test<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>

<p>查看所有名称空间下的所有pod的标签</p>
<p>[root@master1 ~]# <code>kubectl get pods --all-namespaces --show-labels</code></p>
 <pre class="line-numbers language-bash" data-language="bash"><code class="language-bash">NAMESPACE     NAME                                       READY   STATUS             RESTARTS   AGE     LABELS
default       tomcat-test-1                              <span class="token number">1</span>/1     Running            <span class="token number">0</span>          8m10s   <span class="token assign-left variable">app</span><span class="token operator">=</span>tomcat,release<span class="token operator">=</span>v1
kube-system   calico-kube-controllers-6744f6b6d5-nnblc   <span class="token number">1</span>/1     Running            <span class="token number">0</span>          6d21h   k8s-app<span class="token operator">=</span>calico-kube-controllers,pod-template-hash<span class="token operator">=</span>6744f6b6d5
kube-system   calico-node-22g7q                          <span class="token number">1</span>/1     Running            <span class="token number">0</span>          6d21h   controller-revision-hash<span class="token operator">=</span>f646f8d97,k8s-app<span class="token operator">=</span>calico-node,pod-template-generation<span class="token operator">=</span><span class="token number">1</span>
kube-system   calico-node-fvp5g                          <span class="token number">1</span>/1     Running            <span class="token number">0</span>          6d21h   controller-revision-hash<span class="token operator">=</span>f646f8d97,k8s-app<span class="token operator">=</span>calico-node,pod-template-generation<span class="token operator">=</span><span class="token number">1</span>
kube-system   calico-node-wqwwg                          <span class="token number">1</span>/1     Running            <span class="token number">0</span>          6d21h   controller-revision-hash<span class="token operator">=</span>f646f8d97,k8s-app<span class="token operator">=</span>calico-node,pod-template-generation<span class="token operator">=</span><span class="token number">1</span>
kube-system   coredns-7f8cbcb969-2r662                   <span class="token number">1</span>/1     Running            <span class="token number">0</span>          6d22h   k8s-app<span class="token operator">=</span>kube-dns,pod-template-hash<span class="token operator">=</span>7f8cbcb969
kube-system   coredns-7f8cbcb969-kpgsv                   <span class="token number">1</span>/1     Running            <span class="token number">0</span>          6d22h   k8s-app<span class="token operator">=</span>kube-dns,pod-template-hash<span class="token operator">=</span>7f8cbcb969
kube-system   etcd-master1                               <span class="token number">1</span>/1     Running            <span class="token number">0</span>          6d22h   <span class="token assign-left variable">component</span><span class="token operator">=</span>etcd,tier<span class="token operator">=</span>control-plane
kube-system   kube-apiserver-master1                     <span class="token number">1</span>/1     Running            <span class="token number">0</span>          6d22h   <span class="token assign-left variable">component</span><span class="token operator">=</span>kube-apiserver,tier<span class="token operator">=</span>control-plane
kube-system   kube-controller-manager-master1            <span class="token number">1</span>/1     Running            <span class="token number">0</span>          6d22h   <span class="token assign-left variable">component</span><span class="token operator">=</span>kube-controller-manager,tier<span class="token operator">=</span>control-plane
kube-system   kube-proxy-7vjk5                           <span class="token number">1</span>/1     Running            <span class="token number">0</span>          6d22h   controller-revision-hash<span class="token operator">=</span>5cc4b8856c,k8s-app<span class="token operator">=</span>kube-proxy,pod-template-generation<span class="token operator">=</span><span class="token number">1</span>
kube-system   kube-proxy-mfpcg                           <span class="token number">1</span>/1     Running            <span class="token number">0</span>          6d22h   controller-revision-hash<span class="token operator">=</span>5cc4b8856c,k8s-app<span class="token operator">=</span>kube-proxy,pod-template-generation<span class="token operator">=</span><span class="token number">1</span>
kube-system   kube-proxy-pv87q                           <span class="token number">1</span>/1     Running            <span class="token number">0</span>          6d22h   controller-revision-hash<span class="token operator">=</span>5cc4b8856c,k8s-app<span class="token operator">=</span>kube-proxy,pod-template-generation<span class="token operator">=</span><span class="token number">1</span>
kube-system   kube-scheduler-master1                     <span class="token number">1</span>/1     Running            <span class="token number">0</span>          6d22h   <span class="token assign-left variable">component</span><span class="token operator">=</span>kube-scheduler,tier<span class="token operator">=</span>control-plane
<span class="token builtin class-name">test</span>          tomcat-miao                                <span class="token number">0</span>/1     InvalidImageName   <span class="token number">0</span>          3d10h   <span class="token assign-left variable">app</span><span class="token operator">=</span>tomcat-miao
<span class="token builtin class-name">test</span>          tomcat-test1                               <span class="token number">1</span>/1     Running            <span class="token number">0</span>          3d10h   <span class="token assign-left variable">app</span><span class="token operator">=</span>tomcat<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>

<h3 id="二、node节点选择器"><a href="#二、node节点选择器" class="headerlink" title="二、node节点选择器"></a>二、node节点选择器</h3><p>我们在创建pod资源的时候，pod会根据schduler进行调度，那么默认会调度到随机的一个工作节点，如果我们想要pod调度到指定节点或者调度到一些具有相同特点的node节点，怎么办呢？</p>
<p>可以使用pod中的nodeName或者nodeSelector字段指定要调度到的node节点</p>
<p>1、nodeName：</p>
<p>指定pod节点运行在哪个具体node上</p>
<p>把tomcat.tar.gz上传到node1和node2，手动解压：</p>
<pre class="line-numbers language-bash" data-language="bash"><code class="language-bash"><span class="token punctuation">[</span>root@node1 ~<span class="token punctuation">]</span><span class="token comment"># ctr -n=k8s.io images import  tomcat.tar.gz </span>
Loaded image: tomcat:8.5-jre8-alpine
<span class="token punctuation">[</span>root@node2 ~<span class="token punctuation">]</span><span class="token comment"># ctr -n=k8s.io images import  tomcat.tar.gz </span>
Loaded image: tomcat:8.5-jre8-alpine<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span></span></code></pre>

<p>把busybox.tar.gz上传到node1和node2，手动解压：</p>
<pre class="line-numbers language-bash" data-language="bash"><code class="language-bash"><span class="token punctuation">[</span>root@node1 ~<span class="token punctuation">]</span><span class="token comment"># ctr -n=k8s.io images import  busybox.tar.gz</span>
<span class="token punctuation">[</span>root@node2 ~<span class="token punctuation">]</span><span class="token comment"># ctr -n=k8s.io images import  busybox.tar.gz </span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span></span></code></pre>

<p>[root@master1 ~]# cat pod-node.yaml </p>
<pre class="line-numbers language-yaml" data-language="yaml"><code class="language-yaml"><span class="token key atrule">apiVersion</span><span class="token punctuation">:</span> v1
<span class="token key atrule">kind</span><span class="token punctuation">:</span> Pod
<span class="token key atrule">metadata</span><span class="token punctuation">:</span>
  <span class="token key atrule">name</span><span class="token punctuation">:</span> damo<span class="token punctuation">-</span>pod
  <span class="token key atrule">namespace</span><span class="token punctuation">:</span> default
  <span class="token key atrule">labels</span><span class="token punctuation">:</span>
    <span class="token key atrule">app</span><span class="token punctuation">:</span> myapp
    <span class="token key atrule">env</span><span class="token punctuation">:</span> dev
<span class="token key atrule">spec</span><span class="token punctuation">:</span>
  <span class="token key atrule">nodeName</span><span class="token punctuation">:</span> node1
  <span class="token key atrule">containers</span><span class="token punctuation">:</span>
  <span class="token punctuation">-</span> <span class="token key atrule">name</span><span class="token punctuation">:</span> tomcat<span class="token punctuation">-</span>pod<span class="token punctuation">-</span>java
    <span class="token key atrule">image</span><span class="token punctuation">:</span> docker.io/xianchao/tomcat<span class="token punctuation">-</span>8.5<span class="token punctuation">-</span>jre8<span class="token punctuation">:</span>v1
    <span class="token key atrule">imagePullPolicy</span><span class="token punctuation">:</span> IfNotPresent
    <span class="token key atrule">ports</span><span class="token punctuation">:</span>
    <span class="token punctuation">-</span> <span class="token key atrule">containerPort</span><span class="token punctuation">:</span> <span class="token number">8080</span>
  <span class="token punctuation">-</span> <span class="token key atrule">name</span><span class="token punctuation">:</span> busybox
    <span class="token key atrule">image</span><span class="token punctuation">:</span> docker.io/library/busybox<span class="token punctuation">:</span><span class="token number">1.28</span>
    <span class="token key atrule">imagePullPolicy</span><span class="token punctuation">:</span> IfNotPresent
    <span class="token key atrule">command</span><span class="token punctuation">:</span> <span class="token punctuation">[</span><span class="token string">"/bin/sh"</span><span class="token punctuation">]</span>
    <span class="token key atrule">args</span><span class="token punctuation">:</span> <span class="token punctuation">[</span><span class="token string">"-c"</span><span class="token punctuation">,</span><span class="token string">"sleep 3600"</span><span class="token punctuation">]</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>

<p>[root@master1 ~]# <code>kubectl apply -f pod-node.yaml</code></p>
<p>查看pod调度到哪个节点</p>
<pre class="line-numbers language-bash" data-language="bash"><code class="language-bash"><span class="token punctuation">[</span>root@master1 pod<span class="token punctuation">]</span><span class="token comment"># kubectl get pods -owide</span>
NAME       READY   STATUS    RESTARTS   AGE    IP               NODE    NOMINATED NODE   READINESS GATES
damo-pod   <span class="token number">2</span>/2     Running   <span class="token number">0</span>          2m3s   <span class="token number">10.224</span>.166.138   node1   <span class="token operator">&lt;</span>none<span class="token operator">></span>           <span class="token operator">&lt;</span>none<span class="token operator">></span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span></span></code></pre>

<p> 登录node1节点，可以看到有两个容器</p>
<pre class="line-numbers language-bash" data-language="bash"><code class="language-bash"><span class="token punctuation">[</span>root@node1 ~<span class="token punctuation">]</span><span class="token comment"># crictl ps|grep damo</span>
334c2ebe5016e       8c811b4aec35f       <span class="token number">4</span> minutes ago       Running             busybox             <span class="token number">0</span>                   3f12fd106f567       damo-pod
91a65f42c9ba8       4ac473a3dd922       <span class="token number">4</span> minutes ago       Running             tomcat-pod-java     <span class="token number">0</span>                   3f12fd106f567       damo-pod<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span></span></code></pre>

<h4 id="2-1-nodeSelector"><a href="#2-1-nodeSelector" class="headerlink" title="2.1 nodeSelector"></a>2.1 nodeSelector</h4><p>在Pod的spec中可以指定一个nodeSelector对象，它是一个map字典，字典里的每个key-value，分别对应node上的label。</p>
<p>指定pod调度到具有哪些标签的node节点上：</p>
<p>给node节点打标签，打个具有disk&#x3D;ceph的标签</p>
<p>[root@master1 ~]# <code>kubectl label nodes node2 disk=ceph</code></p>
<p>定义pod的时候指定要调度到具有disk&#x3D;ceph标签的node上</p>
<p>[root@master1 ~]# cat pod-1.yaml </p>
<pre class="line-numbers language-yaml" data-language="yaml"><code class="language-yaml"><span class="token key atrule">apiVersion</span><span class="token punctuation">:</span> v1
<span class="token key atrule">kind</span><span class="token punctuation">:</span> Pod
<span class="token key atrule">metadata</span><span class="token punctuation">:</span>
  <span class="token key atrule">name</span><span class="token punctuation">:</span> damo<span class="token punctuation">-</span>pod<span class="token punctuation">-</span><span class="token number">1</span>
  <span class="token key atrule">namespace</span><span class="token punctuation">:</span> default
  <span class="token key atrule">labels</span><span class="token punctuation">:</span>
    <span class="token key atrule">app</span><span class="token punctuation">:</span> myapp
    <span class="token key atrule">env</span><span class="token punctuation">:</span> dev
<span class="token key atrule">spec</span><span class="token punctuation">:</span>
  <span class="token key atrule">nodeSelector</span><span class="token punctuation">:</span>
    <span class="token key atrule">disk</span><span class="token punctuation">:</span> ceph
  <span class="token key atrule">containers</span><span class="token punctuation">:</span>
  <span class="token punctuation">-</span> <span class="token key atrule">name</span><span class="token punctuation">:</span> tomcat<span class="token punctuation">-</span>pod<span class="token punctuation">-</span>java
    <span class="token key atrule">ports</span><span class="token punctuation">:</span>
    <span class="token punctuation">-</span> <span class="token key atrule">containerPort</span><span class="token punctuation">:</span> <span class="token number">8080</span>
    <span class="token key atrule">image</span><span class="token punctuation">:</span> docker.io/xianchao/tomcat<span class="token punctuation">-</span>8.5<span class="token punctuation">-</span>jre8<span class="token punctuation">:</span>v1
    <span class="token key atrule">imagePullPolicy</span><span class="token punctuation">:</span> IfNotPresent<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>

<p>[root@master1 ~]# kubectl apply -f pod-1.yaml</p>
<p>查看pod调度到哪个节点</p>
<pre class="line-numbers language-bash" data-language="bash"><code class="language-bash"><span class="token punctuation">[</span>root@master1 pod<span class="token punctuation">]</span><span class="token comment"># kubectl get pods -owide</span>
NAME         READY   STATUS    RESTARTS   AGE    IP              NODE    NOMINATED NODE   READINESS GATES
damo-pod-1   <span class="token number">1</span>/1     Running   <span class="token number">0</span>          113s   <span class="token number">10.224</span>.104.11   node2   <span class="token operator">&lt;</span>none<span class="token operator">></span>           <span class="token operator">&lt;</span>none<span class="token operator">></span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span></span></code></pre>

<p>做完上面实验，需要把default名称空间下的pod全都删除：kubectl delete pods pod名字</p>
<p>删除node节点打的标签：</p>
<p>[root@master1 ~]# <code>kubectl label nodes node2 disk-</code></p>
<p>假如yaml文件改成如下：</p>
<pre class="line-numbers language-yaml" data-language="yaml"><code class="language-yaml"><span class="token key atrule">apiVersion</span><span class="token punctuation">:</span> v1
<span class="token key atrule">kind</span><span class="token punctuation">:</span> Pod
<span class="token key atrule">metadata</span><span class="token punctuation">:</span>
  <span class="token key atrule">name</span><span class="token punctuation">:</span> damo<span class="token punctuation">-</span>pod<span class="token punctuation">-</span><span class="token number">1</span>
  <span class="token key atrule">namespace</span><span class="token punctuation">:</span> default
  <span class="token key atrule">labels</span><span class="token punctuation">:</span>
    <span class="token key atrule">app</span><span class="token punctuation">:</span> myapp
    <span class="token key atrule">env</span><span class="token punctuation">:</span> dev
<span class="token key atrule">spec</span><span class="token punctuation">:</span>
  <span class="token key atrule">nodeName</span><span class="token punctuation">:</span> node2  <span class="token comment"># 添加节点选择</span>
  <span class="token key atrule">nodeSelector</span><span class="token punctuation">:</span>
    <span class="token key atrule">disk</span><span class="token punctuation">:</span> ceph
  <span class="token key atrule">containers</span><span class="token punctuation">:</span>
  <span class="token punctuation">-</span> <span class="token key atrule">name</span><span class="token punctuation">:</span> tomcat<span class="token punctuation">-</span>pod<span class="token punctuation">-</span>java
    <span class="token key atrule">ports</span><span class="token punctuation">:</span>
    <span class="token punctuation">-</span> <span class="token key atrule">containerPort</span><span class="token punctuation">:</span> <span class="token number">8080</span>
    <span class="token key atrule">image</span><span class="token punctuation">:</span> docker.io/xianchao/tomcat<span class="token punctuation">-</span>8.5<span class="token punctuation">-</span>jre8<span class="token punctuation">:</span>v1
    <span class="token key atrule">imagePullPolicy</span><span class="token punctuation">:</span> IfNotPresent<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>

<p>那么创建pod，会报错，报什么错呢？</p>
<pre class="line-numbers language-bash" data-language="bash"><code class="language-bash"><span class="token punctuation">[</span>root@master1 pod<span class="token punctuation">]</span><span class="token comment"># kubectl apply -f pod-1.yaml </span>
pod/damo-pod-1 created
<span class="token punctuation">[</span>root@master1 pod<span class="token punctuation">]</span><span class="token comment"># kubectl get pods -w</span>
NAME         READY   STATUS         RESTARTS   AGE
damo-pod-1   <span class="token number">0</span>/1     NodeAffinity   <span class="token number">0</span>          19s
<span class="token punctuation">[</span>root@master1 pod<span class="token punctuation">]</span><span class="token comment"># kubectl describe pods damo-pod-1</span>
<span class="token punctuation">..</span><span class="token punctuation">..</span><span class="token punctuation">..</span>
Events:
  Type     Reason        Age   From     Message
  ----     ------        ----  ----     -------
  Warning  NodeAffinity  10m   kubelet  Predicate NodeAffinity failed<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>

<p>同一个yaml文件里定义pod资源，如果同时定义了<strong>nodeName和NodeSelector</strong>，那么条件必须都满足才可以，有一个不满足都会调度失败</p>
<p>假如给node2继续打上标签</p>
<p>[root@master1 ~]# <code>kubectl label nodes node2 disk=ceph</code></p>
<p>如果这个时候直接查看demo-pod-1，状态还是不正常的，需要删除后，再重新apply就可以如下pod状态</p>
<pre class="line-numbers language-bash" data-language="bash"><code class="language-bash"><span class="token punctuation">[</span>root@master1 pod<span class="token punctuation">]</span><span class="token comment"># kubectl delete -f pod-1.yaml </span>
pod <span class="token string">"damo-pod-1"</span> deleted
<span class="token punctuation">[</span>root@master1 pod<span class="token punctuation">]</span><span class="token comment"># kubectl apply -f pod-1.yaml </span>
pod/damo-pod-1 created
<span class="token punctuation">[</span>root@master1 pod<span class="token punctuation">]</span><span class="token comment"># kubectl get pods -owide</span>
NAME         READY   STATUS    RESTARTS   AGE   IP              NODE    NOMINATED NODE   READINESS GATES
damo-pod-1   <span class="token number">1</span>/1     Running   <span class="token number">0</span>          9s    <span class="token number">10.224</span>.104.12   node2   <span class="token operator">&lt;</span>none<span class="token operator">></span>           <span class="token operator">&lt;</span>none<span class="token operator">></span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>

<p>上面可以看到pod能正常调度到node2上</p>
<p>[root@master1 ~]# <code>kubectl label nodes node2 disk-</code></p>
<p>[root@master1]# <code>kubectl delete -f pod-1.yaml</code></p>
<h3 id="三、污点、容忍度、亲和性"><a href="#三、污点、容忍度、亲和性" class="headerlink" title="三、污点、容忍度、亲和性"></a>三、污点、容忍度、亲和性</h3><h4 id="3-1-通俗理解亲和性"><a href="#3-1-通俗理解亲和性" class="headerlink" title="3.1 通俗理解亲和性"></a>3.1 通俗理解亲和性</h4><p>按照惯例，解释一个新名词前，我会拿生活中的例子做类比，方便大家轻松上手。</p>
<p>公司组织员工出去团建，带头人策划了一个小游戏，这个小游戏会将成员分成几个不同的小组进行 PK，最终以团队的比分做为排名依据。</p>
<p>员工可以自由选择队友，这时候就有两种选择的标准：</p>
<ul>
<li>第一种：我有社交恐惧症，只选自己熟悉的同事，自己容易融入。这种就是亲和性原则</li>
<li>第二种：我有社交牛逼症，只选自己陌生的同事，能交到新朋友。这种就是反亲和原则</li>
</ul>
<p>这里的员工就是 K8S 中的 Pod，而“熟悉” 和 “陌生” 就是 Pod 上的标签。</p>
<blockquote>
<p>要注意的是在这里有一点点不一样，因为对于每个人来说这里的标签值是不一样的，而在 Pod 上标签是固定值</p>
</blockquote>
<h4 id="3-2-亲和性调度与-nodeSelector"><a href="#3-2-亲和性调度与-nodeSelector" class="headerlink" title="3.2 亲和性调度与 nodeSelector"></a>3.2 亲和性调度与 nodeSelector</h4><p>以你目前的知识储备来看，应该会认为上面的亲和性做法，和之前学习过的 nodeSelector 很像吧？</p>
<p>仅以上面的例子来看，确实亲和性做法，就是 nodeSelector。</p>
<p>但实际上亲和性调度，远比 nodeSelector 强大许多，还是以上面的亲和性做法来举例</p>
<p>若以 nodeSelector 来实现上面的亲和性原则来组队，那 nodeSelector 就是脑子一根筋，只选自己熟悉的同事，不熟悉的，一概不选。</p>
<p>这么一来，就有可能，所有你熟悉的同事已经被别人捷足先登抢先拉拢了，而最后只剩你一个人孤零零的。</p>
<p>换成亲和性调度，就变得灵活许多，他可以设置两种策略：</p>
<p>对于亲和性和反亲和性，都可以设置：</p>
<ul>
<li>preferredDuringSchedulingIgnoredDuringExecution  &#x3D;&#x3D;&gt; 软策略,表示有节点尽量满足这个位置定义的亲和性，这不是一个必须的条件</li>
<li>requiredDuringSchedulingIgnoredDuringExecution &#x3D;&#x3D;&gt; 硬策略,表示必须有节点满足这个位置定义的亲和性，这是个硬性条件</li>
</ul>
<p>硬策略的做法，就是换个模式的 nodeSelector，它是强制性的，不满足就调度失败。</p>
<p>软策略的做法，则更灵活，可以选择满足条件的，要是真没有满足条件的，就调度到其他节点上（选择自己陌生的同事）</p>
<h4 id="3-3-亲和性的三个种类"><a href="#3-3-亲和性的三个种类" class="headerlink" title="3.3 亲和性的三个种类"></a>3.3 亲和性的三个种类</h4><p>对比 nodeSelector 来说，亲和性调度除非了上面可以选择软策略之外，还有更多强大的功能。</p>
<p>亲和性调度器定义在 <code>.spec.affinity</code> 字段里，通过 explain 命令可以查看其字段</p>
<pre class="line-numbers language-java" data-language="java"><code class="language-java"><span class="token constant">KIND</span><span class="token operator">:</span>     <span class="token class-name">Pod</span>
<span class="token constant">VERSION</span><span class="token operator">:</span>  v1
<span class="token constant">RESOURCE</span><span class="token operator">:</span> affinity <span class="token generics"><span class="token punctuation">&lt;</span><span class="token class-name">Object</span><span class="token punctuation">></span></span>
<span class="token constant">DESCRIPTION</span><span class="token operator">:</span>
     <span class="token class-name">If</span> specified<span class="token punctuation">,</span> the pod's scheduling constraints
     <span class="token class-name">Affinity</span> is a group of affinity scheduling rules<span class="token punctuation">.</span>
<span class="token constant">FIELDS</span><span class="token operator">:</span>
   nodeAffinity    <span class="token generics"><span class="token punctuation">&lt;</span><span class="token class-name">Object</span><span class="token punctuation">></span></span>
     <span class="token class-name">Describes</span> node affinity scheduling rules <span class="token keyword">for</span> the pod<span class="token punctuation">.</span>
   podAffinity    <span class="token generics"><span class="token punctuation">&lt;</span><span class="token class-name">Object</span><span class="token punctuation">></span></span>
     <span class="token class-name">Describes</span> pod affinity scheduling rules <span class="token punctuation">(</span>e<span class="token punctuation">.</span>g<span class="token punctuation">.</span> co<span class="token operator">-</span>locate <span class="token keyword">this</span> pod in the
     same node<span class="token punctuation">,</span> zone<span class="token punctuation">,</span> etc<span class="token punctuation">.</span> as some other <span class="token function">pod</span><span class="token punctuation">(</span>s<span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">.</span>
   podAntiAffinity    <span class="token generics"><span class="token punctuation">&lt;</span><span class="token class-name">Object</span><span class="token punctuation">></span></span>
     <span class="token class-name">Describes</span> pod anti<span class="token operator">-</span>affinity scheduling rules <span class="token punctuation">(</span>e<span class="token punctuation">.</span>g<span class="token punctuation">.</span> avoid putting <span class="token keyword">this</span> pod
     in the same node<span class="token punctuation">,</span> zone<span class="token punctuation">,</span> etc<span class="token punctuation">.</span> as some other <span class="token function">pod</span><span class="token punctuation">(</span>s<span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>

<p>可以看到亲和性调度器，有如下三种：</p>
<ul>
<li>nodeAffinity(node亲和性)：该Pod喜欢调度到什么样的Node上</li>
<li>podAffinity(pod亲和性) ：该Pod喜欢和某些Pod调度在一起</li>
<li>podAntiAffinity(pod反亲和性)：该Pod不喜欢和某些Pod调度在一起</li>
</ul>
<p>上面三种亲和性调度，无论是哪一种，都要依赖标签才能起作用，只是不同的亲和性调度方法，亲和性调度器匹配标签的对象不同</p>
<ul>
<li>node亲和性：检查的是亲和性调度器与node标签的匹配</li>
<li>pod(反)亲和性：检查的是亲和性调度器与pod标签的匹配</li>
</ul>
<h4 id="3-4-node节点亲和性"><a href="#3-4-node节点亲和性" class="headerlink" title="3.4 node节点亲和性"></a>3.4 node节点亲和性</h4><p>node节点亲和性调度：nodeAffinity</p>
<pre class="line-numbers language-bash" data-language="bash"><code class="language-bash"><span class="token punctuation">[</span>root@master1 pod<span class="token punctuation">]</span><span class="token comment"># kubectl explain pods.spec.affinity.nodeAffinity.requiredDuringSchedulingIgnoredDuringExecution.nodeSelectorTerms|grep "&lt;"</span>
RESOURCE: nodeSelectorTerms <span class="token operator">&lt;</span><span class="token punctuation">[</span><span class="token punctuation">]</span>Object<span class="token operator">></span>
   matchExpressions     <span class="token operator">&lt;</span><span class="token punctuation">[</span><span class="token punctuation">]</span>Object<span class="token operator">></span>  <span class="token comment"># 匹配表达式的</span>
   matchFields  <span class="token operator">&lt;</span><span class="token punctuation">[</span><span class="token punctuation">]</span>Object<span class="token operator">></span> <span class="token comment"># 匹配字段的</span>
   
<span class="token punctuation">[</span>root@master1 pod<span class="token punctuation">]</span><span class="token comment"># kubectl explain pods.spec.affinity.nodeAffinity.requiredDuringSchedulingIgnoredDuringExecution.nodeSelectorTerms.matchExpressions|grep "&lt;"</span>
RESOURCE: matchExpressions <span class="token operator">&lt;</span><span class="token punctuation">[</span><span class="token punctuation">]</span>Object<span class="token operator">></span>
   key  <span class="token operator">&lt;</span>string<span class="token operator">></span> -required-   <span class="token comment"># 检查label</span>
   operator     <span class="token operator">&lt;</span>string<span class="token operator">></span> -required-  <span class="token comment"># 做等值选则还是不等值选则</span>
   values       <span class="token operator">&lt;</span><span class="token punctuation">[</span><span class="token punctuation">]</span>string<span class="token operator">></span>  <span class="token comment"># 给定值</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>

<h5 id="3-4-1-使用node亲和性-硬策略"><a href="#3-4-1-使用node亲和性-硬策略" class="headerlink" title="3.4.1 使用node亲和性+硬策略"></a>3.4.1 使用node亲和性+硬策略</h5><p>把myapp-v1.tar.gz上传到node2和node1上，手动解压：</p>
<pre class="line-numbers language-bash" data-language="bash"><code class="language-bash"><span class="token punctuation">[</span>root@node1 ~<span class="token punctuation">]</span><span class="token comment"># ctr -n=k8s.io images import myapp-v1.tar.gz </span>
unpacking docker.io/ikubernetes/myapp:v1 <span class="token punctuation">(</span>sha256:943e72fda89669b10a29723a10f261836dca71aa9b181d362020bb3b715bc9a1<span class="token punctuation">)</span><span class="token punctuation">..</span>.done
<span class="token punctuation">[</span>root@node1 ~<span class="token punctuation">]</span><span class="token comment"># ssh node2 "ctr -n=k8s.io images import myapp-v1.tar.gz"</span>
unpacking docker.io/ikubernetes/myapp:v1 <span class="token punctuation">(</span>sha256:943e72fda89669b10a29723a10f261836dca71aa9b181d362020bb3b715bc9a1<span class="token punctuation">)</span><span class="token punctuation">..</span>.done<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span></span></code></pre>

<p>编写yaml文件</p>
<p>[root@master1 ~]# cat pod-nodeaffinity-demo.yaml </p>
<pre class="line-numbers language-yaml" data-language="yaml"><code class="language-yaml"><span class="token key atrule">apiVersion</span><span class="token punctuation">:</span> v1
<span class="token key atrule">kind</span><span class="token punctuation">:</span> Pod
<span class="token key atrule">metadata</span><span class="token punctuation">:</span>
  <span class="token key atrule">name</span><span class="token punctuation">:</span> pod<span class="token punctuation">-</span>node<span class="token punctuation">-</span>affinity<span class="token punctuation">-</span>demo
  <span class="token key atrule">labels</span><span class="token punctuation">:</span>
    <span class="token key atrule">app</span><span class="token punctuation">:</span> myapp
    <span class="token key atrule">tier</span><span class="token punctuation">:</span> frontend  <span class="token comment"># 层+前端</span>
<span class="token key atrule">spec</span><span class="token punctuation">:</span>
  <span class="token key atrule">affinity</span><span class="token punctuation">:</span>
    <span class="token key atrule">nodeAffinity</span><span class="token punctuation">:</span>
      <span class="token key atrule">requiredDuringSchedulingIgnoredDuringExecution</span><span class="token punctuation">:</span>
        <span class="token key atrule">nodeSelectorTerms</span><span class="token punctuation">:</span>
        <span class="token punctuation">-</span> <span class="token key atrule">matchExpressions</span><span class="token punctuation">:</span>
          <span class="token punctuation">-</span> <span class="token key atrule">key</span><span class="token punctuation">:</span> zone
            <span class="token key atrule">operator</span><span class="token punctuation">:</span> In
            <span class="token key atrule">values</span><span class="token punctuation">:</span>
            <span class="token punctuation">-</span> foo
            <span class="token punctuation">-</span> bar
  <span class="token key atrule">containers</span><span class="token punctuation">:</span>
  <span class="token punctuation">-</span> <span class="token key atrule">name</span><span class="token punctuation">:</span> myapp
    <span class="token key atrule">image</span><span class="token punctuation">:</span> docker.io/ikubernetes/myapp<span class="token punctuation">:</span>v1
    <span class="token key atrule">imagePullPolicy</span><span class="token punctuation">:</span> IfNotPresent<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>

<p>我们检查当前节点中有任意一个节点拥有zone标签的值是foo或者bar，就可以把pod调度到这个node节点的foo或者bar标签上的节点上</p>
<p>[root@master1 ~]# <code>kubectl apply -f pod-nodeaffinity-demo.yaml </code></p>
<p>[root@master1 pod]# <code>kubectl get pods -owide</code></p>
<pre class="line-numbers language-bash" data-language="bash"><code class="language-bash">NAME                     READY   STATUS    RESTARTS   AGE   IP       NODE     NOMINATED NODE   READINESS GATES
pod-node-affinity-demo   <span class="token number">0</span>/1     Pending   <span class="token number">0</span>          38s   <span class="token operator">&lt;</span>none<span class="token operator">></span>   <span class="token operator">&lt;</span>none<span class="token operator">></span>   <span class="token operator">&lt;</span>none<span class="token operator">></span>           <span class="token operator">&lt;</span>none<span class="token operator">></span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span></span></code></pre>

<p>status的状态是pending，上面说明没有完成调度，因为没有一个拥有zone的标签的值是foo或者bar，而且使用的是硬亲和性，必须满足条件才能完成调度</p>
<p>我们现在给节点1添加一个标签</p>
<p>[root@master1 ~]# kubectl label nodes node1 zone&#x3D;foo</p>
<p>给这个node1节点打上标签zone&#x3D;foo，在查看</p>
<pre class="line-numbers language-bash" data-language="bash"><code class="language-bash"><span class="token punctuation">[</span>root@master1 pod<span class="token punctuation">]</span><span class="token comment"># kubectl get nodes -l zone</span>
NAME    STATUS   ROLES   AGE     VERSION
node1   Ready    work1   6d23h   v1.25.0
<span class="token punctuation">[</span>root@master1 pod<span class="token punctuation">]</span><span class="token comment"># kubectl get pods -owide</span>
NAME                     READY   STATUS    RESTARTS   AGE    IP               NODE    NOMINATED NODE   READINESS GATES
pod-node-affinity-demo   <span class="token number">1</span>/1     Running   <span class="token number">0</span>          2m3s   <span class="token number">10.224</span>.166.139   node1   <span class="token operator">&lt;</span>none<span class="token operator">></span>           <span class="token operator">&lt;</span>none<span class="token operator">></span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>

<h5 id="3-4-2-使用node亲和性-软策略"><a href="#3-4-2-使用node亲和性-软策略" class="headerlink" title="3.4.2 使用node亲和性+软策略"></a>3.4.2 使用node亲和性+软策略</h5><p>新建pod-nodeaffinity-demo-2文件</p>
<p>[root@master1]# cat pod-nodeaffinity-demo-2.yaml </p>
<pre class="line-numbers language-yaml" data-language="yaml"><code class="language-yaml"><span class="token key atrule">apiVersion</span><span class="token punctuation">:</span> v1
<span class="token key atrule">kind</span><span class="token punctuation">:</span> Pod
<span class="token key atrule">metadata</span><span class="token punctuation">:</span>
  <span class="token key atrule">name</span><span class="token punctuation">:</span> pod<span class="token punctuation">-</span>node<span class="token punctuation">-</span>affinity<span class="token punctuation">-</span>demo
  <span class="token key atrule">labels</span><span class="token punctuation">:</span>
    <span class="token key atrule">app</span><span class="token punctuation">:</span> myapp
    <span class="token key atrule">tier</span><span class="token punctuation">:</span> frontend  <span class="token comment"># 层+前端</span>
<span class="token key atrule">spec</span><span class="token punctuation">:</span>
  <span class="token key atrule">affinity</span><span class="token punctuation">:</span>
    <span class="token key atrule">nodeAffinity</span><span class="token punctuation">:</span>
      <span class="token key atrule">preferredDuringSchedulingIgnoredDuringExecution</span><span class="token punctuation">:</span>
      <span class="token punctuation">-</span> <span class="token key atrule">weight</span><span class="token punctuation">:</span> <span class="token number">10</span>
        <span class="token key atrule">preference</span><span class="token punctuation">:</span>
          <span class="token key atrule">matchExpressions</span><span class="token punctuation">:</span>
          <span class="token punctuation">-</span> <span class="token key atrule">key</span><span class="token punctuation">:</span> zone1
            <span class="token key atrule">operator</span><span class="token punctuation">:</span> In
            <span class="token key atrule">values</span><span class="token punctuation">:</span>
            <span class="token punctuation">-</span> foo1
            <span class="token punctuation">-</span> bar1
      <span class="token punctuation">-</span> <span class="token key atrule">weight</span><span class="token punctuation">:</span> <span class="token number">20</span>
        <span class="token key atrule">preference</span><span class="token punctuation">:</span>
          <span class="token key atrule">matchExpressions</span><span class="token punctuation">:</span>
          <span class="token punctuation">-</span> <span class="token key atrule">key</span><span class="token punctuation">:</span> zone2
            <span class="token key atrule">operator</span><span class="token punctuation">:</span> In
            <span class="token key atrule">values</span><span class="token punctuation">:</span>
            <span class="token punctuation">-</span> foo2
            <span class="token punctuation">-</span> bar2
  <span class="token key atrule">containers</span><span class="token punctuation">:</span>
  <span class="token punctuation">-</span> <span class="token key atrule">name</span><span class="token punctuation">:</span> myapp
    <span class="token key atrule">image</span><span class="token punctuation">:</span> docker.io/ikubernetes/myapp<span class="token punctuation">:</span>v1
    <span class="token key atrule">imagePullPolicy</span><span class="token punctuation">:</span> IfNotPresent<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>

<p>[root@master1 ~]# kubectl apply -f pod-nodeaffinity-demo-2.yaml</p>
<pre class="line-numbers language-bash" data-language="bash"><code class="language-bash"><span class="token punctuation">[</span>root@master1 pod<span class="token punctuation">]</span><span class="token comment"># kubectl get pods -owide</span>
NAME                     READY   STATUS    RESTARTS   AGE   IP              NODE    NOMINATED NODE   READINESS GATES
pod-node-affinity-demo   <span class="token number">1</span>/1     Running   <span class="token number">0</span>          53s   <span class="token number">10.224</span>.104.13   node2   <span class="token operator">&lt;</span>none<span class="token operator">></span>           <span class="token operator">&lt;</span>none<span class="token operator">></span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span></span></code></pre>

<p>上面说明软亲和性是可以运行这个pod的，尽管没有运行这个pod的节点定义的zone1标签</p>
<p>Node节点亲和性针对的是pod和node的关系，Pod调度到node节点的时候匹配的条件</p>
<h5 id="3-4-3-测试weight权重"><a href="#3-4-3-测试weight权重" class="headerlink" title="3.4.3 测试weight权重"></a>3.4.3 测试weight权重</h5><p>weight是相对权重，权重越高，pod调度的几率越大</p>
<p>假如给node1和node2都打上标签</p>
<pre class="line-numbers language-bash" data-language="bash"><code class="language-bash"><span class="token punctuation">[</span>root@master1 pod<span class="token punctuation">]</span><span class="token comment"># kubectl get pods -owide</span>
NAME                     READY   STATUS    RESTARTS   AGE   IP              NODE    NOMINATED NODE   READINESS GATES
pod-node-affinity-demo   <span class="token number">1</span>/1     Running   <span class="token number">0</span>          53s   <span class="token number">10.224</span>.104.13   node2   <span class="token operator">&lt;</span>none<span class="token operator">></span>           <span class="token operator">&lt;</span>none<span class="token operator">></span>
<span class="token punctuation">[</span>root@master1 pod<span class="token punctuation">]</span><span class="token comment"># kubectl label nodes node1 zone1=foo1</span>
node/node1 labeled
<span class="token punctuation">[</span>root@master1 pod<span class="token punctuation">]</span><span class="token comment"># kubectl label nodes node2 zone2=foo2</span>
node/node2 labeled
<span class="token punctuation">[</span>root@master1 pod<span class="token punctuation">]</span><span class="token comment"># kubectl get pods -owide</span>
NAME                     READY   STATUS    RESTARTS   AGE     IP              NODE    NOMINATED NODE   READINESS GATES
pod-node-affinity-demo   <span class="token number">1</span>/1     Running   <span class="token number">0</span>          2m55s   <span class="token number">10.224</span>.104.13   node2   <span class="token operator">&lt;</span>none<span class="token operator">></span>           <span class="token operator">&lt;</span>none<span class="token operator">></span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>

<p>pod在定义node节点亲和性的时候，node1和node2都满足条件，都可以调度pod，但是node2具有的标签是zone2&#x3D;foo2，pod在匹配zone2&#x3D;foo2的权重高，那么pod就会优先调度到node2上。</p>
<p>删除创建的标签和Pod,为下次实验做准备</p>
<p>[root@master1 ~]# <code>kubectl label nodes node1 zone1-</code></p>
<p>[root@master1 ~]# <code>kubectl label nodes node2 zone2-</code></p>
<p>[root@master1 ~]# <code>kubectl label nodes node1 zone-</code></p>
<p>[root@master1]# <code>kubectl delete -f pod-nodeaffinity-demo.yaml</code></p>
<p>[root@master1]# <code>kubectl delete -f pod-nodeaffinity-demo-2.yaml</code></p>
<p>删除默认名称空间的所有pod资源</p>
<p>[root@master1 pod-2]# <code>kubectl get pods -owide </code></p>
<p>No resources found in default namespace.</p>
<h4 id="3-5-Pod节点亲和性"><a href="#3-5-Pod节点亲和性" class="headerlink" title="3.5 Pod节点亲和性"></a>3.5 Pod节点亲和性</h4><p>pod自身的亲和性调度有两种表示形式</p>
<p>podaffinity：pod和pod更倾向腻在一起，把相近的pod结合到相近的位置，如同一区域，同一机架，这样的话pod和pod之间更好通信，比方说有两个机房，这两个机房部署的集群有1000台主机，那么我们希望把nginx和tomcat都部署同一个地方的node节点上，可以提高通信效率；</p>
<p>podunaffinity：pod和pod更倾向不腻在一起，如果部署两套程序，那么这两套程序更倾向于反亲和性，这样相互之间不会有影响。</p>
<p>第一个pod随机选则一个节点，做为评判后续的pod能否到达这个pod所在的节点上的运行方式，这就称为pod亲和性；</p>
<p>我们怎么判定哪些节点是相同位置的，哪些节点是不同位置的?</p>
<p>我们在定义pod亲和性时需要有一个前提:</p>
<ul>
<li>哪些pod在同一个位置，哪些pod不在同一个位置，</li>
</ul>
<p>这个位置是怎么定义的，标准是什么？</p>
<ul>
<li>以节点名称为标准，这个节点名称相同的表示是同一个位置</li>
<li>节点名称不相同的表示不是一个位置</li>
</ul>
<pre class="line-numbers language-bash" data-language="bash"><code class="language-bash"><span class="token punctuation">[</span>root@node1 ~<span class="token punctuation">]</span><span class="token comment"># kubectl explain pods.spec.affinity.podAffinity.requiredDuringSchedulingIgnoredDuringExecution |grep '&lt;'</span>
RESOURCE: requiredDuringSchedulingIgnoredDuringExecution <span class="token operator">&lt;</span><span class="token punctuation">[</span><span class="token punctuation">]</span>Object<span class="token operator">></span>
     on a <span class="token function">node</span> whose value of the label with key <span class="token operator">&lt;</span>topologyKey<span class="token operator">></span> matches that of
   labelSelector        <span class="token operator">&lt;</span>Object<span class="token operator">></span>  <span class="token comment"># 我们要判断pod跟别的pod亲和，跟哪个pod亲和，需要靠labelSelector，通过labelSelector选则一组能作为亲和对象的pod资源</span>
   namespaceSelector    <span class="token operator">&lt;</span>Object<span class="token operator">></span>
   namespaces   <span class="token operator">&lt;</span><span class="token punctuation">[</span><span class="token punctuation">]</span>string<span class="token operator">></span>
   topologyKey  <span class="token operator">&lt;</span>string<span class="token operator">></span> -required-   <span class="token comment"># 位置拓扑的键，这个是必须字段</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>

<p>怎么判断是不是同一个位置：</p>
<p>rack&#x3D;rack1   使用rack的键是同一个位置</p>
<p>row&#x3D;row1     使用row的键是同一个位置</p>
<p>labelSelector需要选则一组资源，那么这组资源是在哪个名称空间中呢，通过namespace指定，如果不指定namespaces，那么就是当前创建pod的名称空间 </p>
<p><img src="https://raw.githubusercontent.com/miaohsukang/blog-pic/master/wps1-1667910193800-1.jpg"> </p>
<h5 id="3-5-1-pod亲和性-硬策略"><a href="#3-5-1-pod亲和性-硬策略" class="headerlink" title="3.5.1 pod亲和性+硬策略"></a>3.5.1 pod亲和性+硬策略</h5><p>定义两个pod，第一个pod做为基准，第二个pod跟着它走</p>
<p>查看默认名称空间有哪些pod，把看到的pod删除，让默认名称空间没有pod</p>
<p>[root@master1 pod-2]# kubectl get pods</p>
<p>[root@master1 ~]# cat pod-required-affinity-demo-1.yaml </p>
<pre class="line-numbers language-yaml" data-language="yaml"><code class="language-yaml"><span class="token key atrule">apiVersion</span><span class="token punctuation">:</span> v1
<span class="token key atrule">kind</span><span class="token punctuation">:</span> Pod
<span class="token key atrule">metadata</span><span class="token punctuation">:</span>
  <span class="token key atrule">name</span><span class="token punctuation">:</span> pod<span class="token punctuation">-</span>first
  <span class="token key atrule">labels</span><span class="token punctuation">:</span>
    <span class="token key atrule">app2</span><span class="token punctuation">:</span> myapp2
    <span class="token key atrule">titer</span><span class="token punctuation">:</span> frontend
<span class="token key atrule">spec</span><span class="token punctuation">:</span>
  <span class="token key atrule">containers</span><span class="token punctuation">:</span>
  <span class="token punctuation">-</span> <span class="token key atrule">name</span><span class="token punctuation">:</span> myapp
    <span class="token key atrule">image</span><span class="token punctuation">:</span> docker.io/ikubernetes/myapp<span class="token punctuation">:</span>v1
    <span class="token key atrule">imagePullPolicy</span><span class="token punctuation">:</span> IfNotPresent<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>

<p>[root@master1 ]# kubectl apply -f pod-required-affinity-demo-1.yaml</p>
<p>现在开始创建第二个damo文件</p>
<p>[root@master1 ~]# cat pod-required-affinity-demo-2.yaml</p>
<pre class="line-numbers language-yaml" data-language="yaml"><code class="language-yaml"><span class="token key atrule">apiVersion</span><span class="token punctuation">:</span> v1
<span class="token key atrule">kind</span><span class="token punctuation">:</span> Pod
<span class="token key atrule">metadata</span><span class="token punctuation">:</span>
  <span class="token key atrule">name</span><span class="token punctuation">:</span> pod<span class="token punctuation">-</span>second
  <span class="token key atrule">labels</span><span class="token punctuation">:</span>
    <span class="token key atrule">app2</span><span class="token punctuation">:</span> backup
    <span class="token key atrule">titer</span><span class="token punctuation">:</span> db
<span class="token key atrule">spec</span><span class="token punctuation">:</span>
  <span class="token key atrule">containers</span><span class="token punctuation">:</span>
  <span class="token punctuation">-</span> <span class="token key atrule">name</span><span class="token punctuation">:</span> busybox
    <span class="token key atrule">image</span><span class="token punctuation">:</span> docker.io/library/busybox<span class="token punctuation">:</span><span class="token number">1.28</span>
    <span class="token key atrule">imagePullPolicy</span><span class="token punctuation">:</span> IfNotPresent
    <span class="token key atrule">command</span><span class="token punctuation">:</span> <span class="token punctuation">[</span><span class="token string">"sh"</span><span class="token punctuation">,</span><span class="token string">"-c"</span><span class="token punctuation">,</span><span class="token string">"sleep 3600"</span><span class="token punctuation">]</span>
  <span class="token key atrule">affinity</span><span class="token punctuation">:</span>
    <span class="token key atrule">podAffinity</span><span class="token punctuation">:</span>
      <span class="token key atrule">requiredDuringSchedulingIgnoredDuringExecution</span><span class="token punctuation">:</span>
      <span class="token punctuation">-</span> <span class="token key atrule">labelSelector</span><span class="token punctuation">:</span>
          <span class="token key atrule">matchExpressions</span><span class="token punctuation">:</span>
          <span class="token punctuation">-</span> <span class="token punctuation">&#123;</span><span class="token key atrule">key</span><span class="token punctuation">:</span> app2<span class="token punctuation">,</span><span class="token key atrule">operator</span><span class="token punctuation">:</span> In<span class="token punctuation">,</span><span class="token key atrule">values</span><span class="token punctuation">:</span> <span class="token punctuation">[</span><span class="token string">"myapp2"</span><span class="token punctuation">]</span><span class="token punctuation">&#125;</span>
          <span class="token comment">#- key: app2</span>
          <span class="token comment">#  operator: In</span>
          <span class="token comment">#  values: </span>
          <span class="token comment">#  - myapp2</span>
        <span class="token key atrule">topologyKey</span><span class="token punctuation">:</span> kubernetes.io/hostname<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>

<p>上面表示创建的pod必须与拥有app&#x3D;myapp标签的pod在一个节点上</p>
<p>[root@master1 ~]# kubectl apply -f pod-required-affinity-demo-2.yaml </p>
<pre class="line-numbers language-bash" data-language="bash"><code class="language-bash"><span class="token punctuation">[</span>root@node1 ~<span class="token punctuation">]</span><span class="token comment"># kubectl get pods -owide</span>
NAME         READY   STATUS    RESTARTS   AGE     IP              NODE    NOMINATED NODE   READINESS GATES
pod-first    <span class="token number">1</span>/1     Running   <span class="token number">0</span>          9m38s   <span class="token number">10.224</span>.104.14   node2   <span class="token operator">&lt;</span>none<span class="token operator">></span>           <span class="token operator">&lt;</span>none<span class="token operator">></span>
pod-second   <span class="token number">1</span>/1     Running   <span class="token number">0</span>          44s     <span class="token number">10.224</span>.104.15   node2   <span class="token operator">&lt;</span>none<span class="token operator">></span>           <span class="token operator">&lt;</span>none<span class="token operator">></span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span></span></code></pre>

<p>上面说明第一个pod调度到哪，第二个pod也调度到哪，这就是pod节点亲和性</p>
<p>[root@master1 ~]# kubectl delete -f pod-required-affinity-demo-1.yaml</p>
<p>[root@master1 ~]# kubectl delete -f pod-required-affinity-demo-2.yaml</p>
<h5 id="3-5-2-pod反亲和性-硬策略"><a href="#3-5-2-pod反亲和性-硬策略" class="headerlink" title="3.5.2 pod反亲和性+硬策略"></a>3.5.2 pod反亲和性+硬策略</h5><p>定义两个pod，第一个pod做为基准，第二个pod跟它调度节点相反</p>
<p>[root@master1 ~]# cat pod-required-anti-affinity-demo-1.yaml</p>
<pre class="line-numbers language-yaml" data-language="yaml"><code class="language-yaml"><span class="token key atrule">apiVersion</span><span class="token punctuation">:</span> v1
<span class="token key atrule">kind</span><span class="token punctuation">:</span> Pod
<span class="token key atrule">metadata</span><span class="token punctuation">:</span>
  <span class="token key atrule">name</span><span class="token punctuation">:</span> pod<span class="token punctuation">-</span>first
  <span class="token key atrule">labels</span><span class="token punctuation">:</span>
    <span class="token key atrule">app1</span><span class="token punctuation">:</span> myapp1
    <span class="token key atrule">titer</span><span class="token punctuation">:</span> frontend
<span class="token key atrule">spec</span><span class="token punctuation">:</span>
  <span class="token key atrule">containers</span><span class="token punctuation">:</span>
  <span class="token punctuation">-</span> <span class="token key atrule">name</span><span class="token punctuation">:</span> myapp
    <span class="token key atrule">image</span><span class="token punctuation">:</span> docker.io/ikubernetes/myapp<span class="token punctuation">:</span>v1
    <span class="token key atrule">imagePullPolicy</span><span class="token punctuation">:</span> IfNotPresent<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>

<p>[root@master1 ~]# kubectl apply -f  pod-required-anti-affinity-demo-1.yaml</p>
<p>开始创建第二个Pod文件 </p>
<p>[root@master1 ~]# cat pod-required-anti-affinity-demo-2.yaml</p>
<pre class="line-numbers language-yaml" data-language="yaml"><code class="language-yaml"><span class="token key atrule">apiVersion</span><span class="token punctuation">:</span> v1
<span class="token key atrule">kind</span><span class="token punctuation">:</span> Pod
<span class="token key atrule">metadata</span><span class="token punctuation">:</span>
  <span class="token key atrule">name</span><span class="token punctuation">:</span> pod<span class="token punctuation">-</span>second
  <span class="token key atrule">labels</span><span class="token punctuation">:</span>
    <span class="token key atrule">app2</span><span class="token punctuation">:</span> backup
    <span class="token key atrule">titer</span><span class="token punctuation">:</span> db
<span class="token key atrule">spec</span><span class="token punctuation">:</span>
  <span class="token key atrule">containers</span><span class="token punctuation">:</span>
  <span class="token punctuation">-</span> <span class="token key atrule">name</span><span class="token punctuation">:</span> busybox
    <span class="token key atrule">image</span><span class="token punctuation">:</span> docker.io/library/busybox<span class="token punctuation">:</span><span class="token number">1.28</span>
    <span class="token key atrule">imagePullPolicy</span><span class="token punctuation">:</span> IfNotPresent
    <span class="token key atrule">command</span><span class="token punctuation">:</span> <span class="token punctuation">[</span><span class="token string">"sh"</span><span class="token punctuation">,</span><span class="token string">"-c"</span><span class="token punctuation">,</span><span class="token string">"sleep 3600"</span><span class="token punctuation">]</span>
  <span class="token key atrule">affinity</span><span class="token punctuation">:</span>
    <span class="token key atrule">podAntiAffinity</span><span class="token punctuation">:</span>
      <span class="token key atrule">requiredDuringSchedulingIgnoredDuringExecution</span><span class="token punctuation">:</span>
      <span class="token punctuation">-</span> <span class="token key atrule">labelSelector</span><span class="token punctuation">:</span>
          <span class="token key atrule">matchExpressions</span><span class="token punctuation">:</span>
          <span class="token punctuation">-</span> <span class="token punctuation">&#123;</span><span class="token key atrule">key</span><span class="token punctuation">:</span> app1<span class="token punctuation">,</span><span class="token key atrule">operator</span><span class="token punctuation">:</span> In<span class="token punctuation">,</span><span class="token key atrule">values</span><span class="token punctuation">:</span> <span class="token punctuation">[</span><span class="token string">"myapp1"</span><span class="token punctuation">]</span><span class="token punctuation">&#125;</span>
        <span class="token key atrule">topologyKey</span><span class="token punctuation">:</span> kubernetes.io/hostname<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>

<p>[root@master1 ~]# kubectl apply -f pod-required-anti-affinity-demo-2.yaml</p>
<pre class="line-numbers language-bash" data-language="bash"><code class="language-bash"><span class="token punctuation">[</span>root@master1 ~<span class="token punctuation">]</span><span class="token comment"># kubectl get pods -owide # 显示两个pod不在一个node节点上，这就是pod节点反亲和性</span>
NAME         READY   STATUS    RESTARTS   AGE   IP               NODE    NOMINATED NODE   READINESS GATES
pod-first    <span class="token number">1</span>/1     Running   <span class="token number">0</span>          7m    <span class="token number">10.224</span>.104.17    node2   <span class="token operator">&lt;</span>none<span class="token operator">></span>           <span class="token operator">&lt;</span>none<span class="token operator">></span>
pod-second   <span class="token number">1</span>/1     Running   <span class="token number">0</span>          3s    <span class="token number">10.224</span>.166.140   node1   <span class="token operator">&lt;</span>none<span class="token operator">></span>           <span class="token operator">&lt;</span>none<span class="token operator">></span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span></span></code></pre>

<p>[root@master1 ~]# kubectl delete -f pod-required-anti-affinity-demo-1.yaml</p>
<p>[root@master1 ~]# kubectl delete -f pod-required-anti-affinity-demo-2.yaml</p>
<h5 id="3-5-3-域和匹配方式"><a href="#3-5-3-域和匹配方式" class="headerlink" title="3.5.3 域和匹配方式"></a>3.5.3 域和匹配方式</h5><p>在上面的诸多示例中，为了方便：</p>
<ul>
<li>域都是使用的是 kubernetes.io&#x2F;hostname</li>
<li>并且都是使用的 matchExpressions</li>
</ul>
<p>关于第一个问题，实际上K8S 有内置三种域的 key，分别是 <code>kubernetes.io/hostname</code>和 <code>topology.kubernetes.io/region</code> 和 <code>topology.kubernetes.io/zone</code>，最常用的就是 <code>kubernetes.io/hostname</code> ，大家可根据自身需求进行选择。</p>
<p>通常在节点初始化的时候，controller-manager 就会为节点打上许多 label，比如 <code>kubernetes.io/hostname</code> 表示节点的 hostname 来区分节点；另外，在云厂商提供的 k8s 服务，或者使用 cloud-controller-manager 的自建集群，通常还会给节点打上 <code>failure-domain.beta.kubernetes.io/zone</code> 和 <code>failure-domain.beta.kubernetes.io/region</code> 以区分节点所在可用区和所在地域，但自 v1.17 开始将会改名成 <code>topology.kubernetes.io/zone</code> 和 <code>topology.kubernetes.io/region</code>,叫拓扑感知服务路由</p>
<p>关于第二个问题，上面使用 matchExpressions 是更通用、更灵活的方式，因为 matchExpressions 可以利用操作符（operator）做更多复杂的判断</p>
<p>下面是操作的可选项及其含义：</p>
<ul>
<li>In：label的值在某个列表中</li>
<li>NotIn：label的值不在某个列表中</li>
<li>Gt：label的值大于某个值</li>
<li>Lt：label的值小于某个值</li>
<li>Exists：某个label存在</li>
<li>DoesNotExist：某个label 存在</li>
</ul>
<p>不同调度器支持的操作符不太一样，可参考下面表格</p>
<p>若只是单纯的 security&#x3D;S1，可以直接使用 matchLabels ，书写更加快捷，也更容易理解</p>
<p>因此下面两种方法在效果上是等价的</p>
<pre class="line-numbers language-yaml" data-language="yaml"><code class="language-yaml"><span class="token comment"># 第一种写法：使用 matchExpressions</span>
      <span class="token key atrule">affinity</span><span class="token punctuation">:</span>
        <span class="token key atrule">podAffinity</span><span class="token punctuation">:</span>
          <span class="token key atrule">requiredDuringSchedulingIgnoredDuringExecution</span><span class="token punctuation">:</span>  <span class="token comment"># 硬策略</span>
            <span class="token key atrule">labelSelector</span><span class="token punctuation">:</span>
              <span class="token key atrule">matchExpressions</span><span class="token punctuation">:</span>
              <span class="token punctuation">-</span> <span class="token key atrule">key</span><span class="token punctuation">:</span> security
                <span class="token key atrule">operator</span><span class="token punctuation">:</span> In
                <span class="token key atrule">values</span><span class="token punctuation">:</span>
                <span class="token punctuation">-</span> S1
          <span class="token key atrule">topologyKey</span><span class="token punctuation">:</span> <span class="token string">"kubernetes.io/hostname"</span>

<span class="token comment"># 第二种写法：使用 matchLabels</span>
      <span class="token key atrule">affinity</span><span class="token punctuation">:</span>
        <span class="token key atrule">podAffinity</span><span class="token punctuation">:</span>
          <span class="token key atrule">requiredDuringSchedulingIgnoredDuringExecution</span><span class="token punctuation">:</span>  <span class="token comment"># 硬策略</span>
            <span class="token key atrule">labelSelector</span><span class="token punctuation">:</span>
              <span class="token key atrule">matchLabels</span><span class="token punctuation">:</span>
                <span class="token key atrule">security</span><span class="token punctuation">:</span> S1
          <span class="token key atrule">topologyKey</span><span class="token punctuation">:</span> <span class="token string">"kubernetes.io/hostname"</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>

<p>换一个topologykey</p>
<p>[root@master1 ~]# kubectl label nodes  node2  zone&#x3D;foo</p>
<p>[root@master1 ~]# kubectl label nodes  node1  zone&#x3D;foo </p>
<p>[root@master1]# cat pod-first-required-anti-affinity-demo-1.yaml</p>
<pre class="line-numbers language-yaml" data-language="yaml"><code class="language-yaml"><span class="token key atrule">apiVersion</span><span class="token punctuation">:</span> v1
<span class="token key atrule">kind</span><span class="token punctuation">:</span> Pod
<span class="token key atrule">metadata</span><span class="token punctuation">:</span>
  <span class="token key atrule">name</span><span class="token punctuation">:</span> pod<span class="token punctuation">-</span>first
  <span class="token key atrule">labels</span><span class="token punctuation">:</span>
    <span class="token key atrule">app3</span><span class="token punctuation">:</span> myapp3
    <span class="token key atrule">tier</span><span class="token punctuation">:</span> frontend
<span class="token key atrule">spec</span><span class="token punctuation">:</span>
  <span class="token key atrule">containers</span><span class="token punctuation">:</span>
  <span class="token punctuation">-</span> <span class="token key atrule">name</span><span class="token punctuation">:</span> myapp
    <span class="token key atrule">image</span><span class="token punctuation">:</span> docker.io/ikubernetes/myapp<span class="token punctuation">:</span>v1
    <span class="token key atrule">imagePullPolicy</span><span class="token punctuation">:</span> IfNotPresent<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>

<p>[root@master1 affinity]# kubectl apply -f pod-first-required-anti-affinity-demo-1.yaml</p>
<p>[root@master1]# cat pod-second-required-anti-affinity-demo-1.yaml </p>
<pre class="line-numbers language-yaml" data-language="yaml"><code class="language-yaml"><span class="token key atrule">apiVersion</span><span class="token punctuation">:</span> v1
<span class="token key atrule">kind</span><span class="token punctuation">:</span> Pod
<span class="token key atrule">metadata</span><span class="token punctuation">:</span>
  <span class="token key atrule">name</span><span class="token punctuation">:</span> pod<span class="token punctuation">-</span>second
  <span class="token key atrule">labels</span><span class="token punctuation">:</span>
    <span class="token key atrule">app</span><span class="token punctuation">:</span> backend
    <span class="token key atrule">tier</span><span class="token punctuation">:</span> db
<span class="token key atrule">spec</span><span class="token punctuation">:</span>
  <span class="token key atrule">containers</span><span class="token punctuation">:</span>
  <span class="token punctuation">-</span> <span class="token key atrule">name</span><span class="token punctuation">:</span> busybox
    <span class="token key atrule">image</span><span class="token punctuation">:</span> busybox<span class="token punctuation">:</span>latest
    <span class="token key atrule">imagePullPolicy</span><span class="token punctuation">:</span> IfNotPresent
    <span class="token key atrule">command</span><span class="token punctuation">:</span> <span class="token punctuation">[</span><span class="token string">"sh"</span><span class="token punctuation">,</span><span class="token string">"-c"</span><span class="token punctuation">,</span><span class="token string">"sleep 3600"</span><span class="token punctuation">]</span>
  <span class="token key atrule">affinity</span><span class="token punctuation">:</span>
    <span class="token key atrule">podAntiAffinity</span><span class="token punctuation">:</span>
      <span class="token key atrule">requiredDuringSchedulingIgnoredDuringExecution</span><span class="token punctuation">:</span>
      <span class="token punctuation">-</span> <span class="token key atrule">labelSelector</span><span class="token punctuation">:</span>
          <span class="token key atrule">matchExpressions</span><span class="token punctuation">:</span>
          <span class="token punctuation">-</span> <span class="token punctuation">&#123;</span><span class="token key atrule">key</span><span class="token punctuation">:</span> app3 <span class="token punctuation">,</span><span class="token key atrule">operator</span><span class="token punctuation">:</span> In<span class="token punctuation">,</span> <span class="token key atrule">values</span><span class="token punctuation">:</span> <span class="token punctuation">[</span><span class="token string">"myapp3"</span><span class="token punctuation">]</span><span class="token punctuation">&#125;</span>
        <span class="token key atrule">topologyKey</span><span class="token punctuation">:</span>  zone<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>

<p>[root@master1 ~]# kubectl apply -f pod-second-required-anti-affinity-demo-1.yaml</p>
<pre class="line-numbers language-bash" data-language="bash"><code class="language-bash"><span class="token punctuation">[</span>root@master1 ~<span class="token punctuation">]</span><span class="token comment"># kubectl get pods -owide</span>
NAME         READY   STATUS    RESTARTS   AGE     IP              NODE     NOMINATED NODE   READINESS GATES
pod-first    <span class="token number">1</span>/1     Running   <span class="token number">0</span>          6m12s   <span class="token number">10.224</span>.104.19   node2    <span class="token operator">&lt;</span>none<span class="token operator">></span>           <span class="token operator">&lt;</span>none<span class="token operator">></span>
pod-second   <span class="token number">0</span>/1     Pending   <span class="token number">0</span>          60s     <span class="token operator">&lt;</span>none<span class="token operator">></span>          <span class="token operator">&lt;</span>none<span class="token operator">></span>   <span class="token operator">&lt;</span>none<span class="token operator">></span>           <span class="token operator">&lt;</span>none<span class="token operator">></span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span></span></code></pre>

<p>[root@master1]# kubectl delete -f pod-first-required-anti-affinity-demo-1.yaml </p>
<p>[root@master1]# kubectl delete -f pod-second-required-anti-affinity-demo-1.yaml</p>
<p>[root@master1 ~]#  kubectl label nodes  node1  zone-</p>
<p>[root@master1 ~]#  kubectl label nodes  node2  zone-</p>
<p>第二个pod是pending，因为两个节点是同一个位置，现在没有不是同一个位置的了，而且我们要求反亲和性，所以就会处于pending状态，如果在反亲和性这个位置把required改成preferred，那么也会运行。</p>
<p>podaffinity：pod节点亲和性，pod倾向于哪个pod</p>
<p>poduntiaffinity:pod反亲和性</p>
<p>nodeaffinity：node节点亲和性，pod倾向于哪个node</p>
<h3 id="四、污点、容忍度"><a href="#四、污点、容忍度" class="headerlink" title="四、污点、容忍度"></a>四、污点、容忍度</h3><h4 id="4-1-污点与标签区别"><a href="#4-1-污点与标签区别" class="headerlink" title="4.1 污点与标签区别"></a>4.1 污点与标签区别</h4><p>污点与标签有什么不同呢？这是我们在学习污点时首先要搞清楚的问题。</p>
<p>由于标签和污点工作原理的不同，他们的适用场景也不一样。</p>
<p><strong>标签</strong>通常用于为Pod指定分组，规定了Pod只能调度到这些分组里的node中，这是一种强制的做法。</p>
<p><strong>污点</strong>通常用于将Node设置为专用节点，默认情况下普通Pod是无法调度过来，仅当你在Pod中指定了对应的容忍度才能调度。</p>
<p>给了节点选择的主动权，我们给节点打一个污点，不容忍的pod就运行不上来，污点就是定义在节点上的键值属性数据，可以定决定拒绝那些pod；</p>
<p><strong>taints</strong>是键值数据，用在节点上，定义污点；</p>
<p><strong>tolerations</strong>是键值数据，用在pod上，定义容忍度，能容忍哪些污点</p>
<p>pod亲和性是pod属性；但是污点是节点的属性，污点定义在k8s集群的节点上的一个字段</p>
<h4 id="4-2-容忍度与污点"><a href="#4-2-容忍度与污点" class="headerlink" title="4.2 容忍度与污点"></a>4.2 容忍度与污点</h4><p>污点也是打在Node上，可以理解为对外公开自己的“缺点”（并非真的缺点），想调度到我这边的，请明示说出你可以容忍我的缺点的，不然是调度不过来的。</p>
<p>使用如下命令为 worker02 打上污点，而worker01却没有任何污点</p>
<pre class="line-numbers language-bash" data-language="bash"><code class="language-bash">kubectl taint nodes worker02 <span class="token assign-left variable">gpu</span><span class="token operator">=</span>true:NoSchedule<span aria-hidden="true" class="line-numbers-rows"><span></span></span></code></pre>

<p>正常你创建的Pod，没有进行特殊的配置，是无法调度到worker02的，只能调度到worker01，即使你使用了nodeSelector</p>
<p><img src="https://raw.githubusercontent.com/miaohsukang/blog-pic/master/640.png"></p>
<p>只有你在Po 上加上了如下的容忍度（在 <code>.spec</code> 下），才能<strong>有可能地</strong>创建到worker02上</p>
<pre class="line-numbers language-yaml" data-language="yaml"><code class="language-yaml"><span class="token key atrule">tolerations</span><span class="token punctuation">:</span>
<span class="token punctuation">-</span> <span class="token key atrule">key</span><span class="token punctuation">:</span> <span class="token string">"gpu"</span>
  <span class="token key atrule">operator</span><span class="token punctuation">:</span> <span class="token string">"Equal"</span>
  <span class="token key atrule">value</span><span class="token punctuation">:</span> <span class="token string">"true"</span>
  <span class="token key atrule">effect</span><span class="token punctuation">:</span> <span class="token string">"NoSchedule"</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span></span></code></pre>

<p>千万要注意的是，上面说的是有可能，而不是一定。</p>
<p>如果要标准地调度到 gpu 的机器上，还要配合前面的–如何打标签？-</p>
<ul>
<li>标签：实现精度地调度的需求</li>
<li>污点：避免产生不必要地浪费</li>
</ul>
<p><img src="https://raw.githubusercontent.com/miaohsukang/blog-pic/master/640-1668001890512-1.png"></p>
<p>上面节点上的污点的，我再翻译一下，意思是该机器上有 GPU，没有指定需要 GPU 的容忍度 的 Pod ，不能调度过来。</p>
<p>如果不使用 GPU 的 Pod 也创建到有 GPU 的节点上，那就是浪费资源，这是不能理解也不能允许的。</p>
<p>而后面的容忍度，意思是，我可以调度到有 gpu 的机器上，如果没有指定这个配置，就无法调度到 GPU 的机器上。</p>
<p>如果要删除原有的污点，可以在上面添加污点的命令最后加个减号 <code>-</code></p>
<pre class="line-numbers language-bash" data-language="bash"><code class="language-bash">kubectl taint nodes worker02 <span class="token assign-left variable">gpu</span><span class="token operator">=</span>true:NoSchedule-<span aria-hidden="true" class="line-numbers-rows"><span></span></span></code></pre>

<h4 id="4-3-容忍度的配置"><a href="#4-3-容忍度的配置" class="headerlink" title="4.3 容忍度的配置"></a>4.3 容忍度的配置</h4><p>容忍度，由几个关键字段组成：</p>
<ul>
<li>key：键（必填）</li>
<li>value：值，当operator为Equal，value必填，当operator为 Exists，value就不用填写</li>
<li>operator：操作，可以为 Exists （存在即可匹配）或者Equal（value必须与相等才算匹配）</li>
<li>effect：影响，有三个选项：NoSchedule、PreferNoSchedule、NoExecute</li>
</ul>
<p>其中 effect 比较难理解，这边挑出来专门说一下，要理解effect，就要理解容忍度与污点的过滤原理。</p>
<p>简单来说，一个Node上可以设置多个污点，一个Pod也可以设置多个容忍度。</p>
<p>Kubernetes处理多个污点和容忍度的过程就像一个过滤器：从一个节点的所有污点开始遍历，过滤掉那些Pod中存在与之相匹配的容忍度的污点。余下未被过滤的污点的effect值决定了Pod是否会被分配到该节点，特别是以下情况：</p>
<ul>
<li>如果未被过滤的污点中存在至少一个 effect 值为 NoSchedule 的污点， 则 Kubernetes 不会将 Pod 分配到该节点。</li>
<li>如果未被过滤的污点中不存在 effect 值为 NoSchedule 的污点， 但是存在 effect 值为 PreferNoSchedule 的污点， 则 Kubernetes 会尝试不将Pod分配到该节点。</li>
<li>如果未被过滤的污点中存在至少一个 effect 值为 NoExecute 的污点， 则 Kubernetes 不会将 Pod 分配到该节点（如果 Pod 还未在节点上运行）， 或者将 Pod 从该节点驱逐（如果 Pod 已经在节点上运行）。</li>
</ul>
<p>taints的effect用来定义对pod对象的排斥等级（效果）：</p>
<ul>
<li>NoSchedule：表示k8s将不会将Pod调度到具有该污点的Node上</li>
</ul>
<p>仅影响pod调度过程，当pod能容忍这个节点污点，就可以调度到当前节点，后来这个节点的污点改了，加了一个新的污点，使得之前调度的pod不能容忍了，那这个pod会怎么处理，对现存的pod对象不产生影响</p>
<ul>
<li>NoExecute：表示k8s将不会将Pod调度到具有该污点的Node上，同时会将Node上已经存在的Pod驱逐出去</li>
</ul>
<p>既影响调度过程，又影响现存的pod对象，如果现存的pod不能容忍节点后来加的污点，这个pod就会被驱逐</p>
<ul>
<li>PreferNoSchedule：表示k8s将尽量避免将Pod调度到具有该污点的Node上</li>
</ul>
<p>最好不，也可以，是NoSchedule的柔性版本</p>
<h4 id="4-4-污点的原生用途"><a href="#4-4-污点的原生用途" class="headerlink" title="4.4 污点的原生用途"></a>4.4 污点的原生用途</h4><p>在原生的kubernetes中是如何使用污点的呢？</p>
<p>在kubernetes的每个集群节点上，都有一个kubelet服务，它会监控集群节点的CPU、内存、磁盘空间和文件系统的inode等资源。</p>
<p>当这些资源中的一个或者多个达到特定的消耗水平， kubelet会主动给节点打上一个或者多个污点标记，这些标记的effect为 <strong>NoExecute</strong></p>
<p>比如内存比较紧张的话，会打上 node.kubernetes.io&#x2F;memory-pressure</p>
<p>比如磁盘比较紧张的话，会打上 node.kubernetes.io&#x2F;disk-pressure</p>
<p>比如 pid 比较紧张的话，会打上 node.kubernetes.io&#x2F;pid-pressure</p>
<p>而如果该节点上，已有一些 Pod 在运行，并且这些 Pod 没有配置以上三种对应的容忍度，则kubelet会开始驱逐的流程，一个一个的驱逐，直到节点不再有存在资源压力为止，才会清除污点，结束驱逐。</p>
<p>通常还会带上一个<code>tolerationSeconds</code>，它意思是在污点出现后，Pod还可以正常工作多少时间，也就是延迟多久再进行驱逐。</p>
<p>除了以上污点之外，还有其他常见的</p>
<ul>
<li>node.kubernetes.io&#x2F;not-ready：节点未准备好。这相当于节点状态 Ready 的值为 “False”</li>
<li>node.kubernetes.io&#x2F;unreachable：节点控制器访问不到节点. 这相当于节点状态 Ready 的值为 “Unknown”。</li>
<li>node.kubernetes.io&#x2F;network-unavailable：节点网络不可用。</li>
<li>node.kubernetes.io&#x2F;unschedulable: 节点不可调度。</li>
</ul>
<p>而这些污点的 effect 通常为<strong>NoSchedule</strong>，以防新的Pod调度过来，却无法正常工作。</p>
<h4 id="4-5-实验操作"><a href="#4-5-实验操作" class="headerlink" title="4.5 实验操作"></a>4.5 实验操作</h4><p>查看kube-system名称空间上运行哪些pod</p>
<pre class="line-numbers language-bash" data-language="bash"><code class="language-bash"><span class="token punctuation">[</span>root@master1 ~<span class="token punctuation">]</span><span class="token comment"># kubectl get pods -n kube-system -owide</span>
NAME                                       READY   STATUS    RESTARTS   AGE     IP              NODE      NOMINATED NODE   READINESS GATES
calico-kube-controllers-6744f6b6d5-nnblc   <span class="token number">1</span>/1     Running   <span class="token number">0</span>          7d23h   <span class="token number">10.224</span>.104.1    node2     <span class="token operator">&lt;</span>none<span class="token operator">></span>           <span class="token operator">&lt;</span>none<span class="token operator">></span>
calico-node-22g7q                          <span class="token number">1</span>/1     Running   <span class="token number">0</span>          7d23h   <span class="token number">10.10</span>.30.182    node2     <span class="token operator">&lt;</span>none<span class="token operator">></span>           <span class="token operator">&lt;</span>none<span class="token operator">></span>
calico-node-fvp5g                          <span class="token number">1</span>/1     Running   <span class="token number">0</span>          7d23h   <span class="token number">10.10</span>.30.181    node1     <span class="token operator">&lt;</span>none<span class="token operator">></span>           <span class="token operator">&lt;</span>none<span class="token operator">></span>
calico-node-wqwwg                          <span class="token number">1</span>/1     Running   <span class="token number">0</span>          7d23h   <span class="token number">10.10</span>.30.180    master1   <span class="token operator">&lt;</span>none<span class="token operator">></span>           <span class="token operator">&lt;</span>none<span class="token operator">></span>
coredns-7f8cbcb969-2r662                   <span class="token number">1</span>/1     Running   <span class="token number">0</span>          8d      <span class="token number">10.224</span>.137.65   master1   <span class="token operator">&lt;</span>none<span class="token operator">></span>           <span class="token operator">&lt;</span>none<span class="token operator">></span>
coredns-7f8cbcb969-kpgsv                   <span class="token number">1</span>/1     Running   <span class="token number">0</span>          8d      <span class="token number">10.224</span>.137.66   master1   <span class="token operator">&lt;</span>none<span class="token operator">></span>           <span class="token operator">&lt;</span>none<span class="token operator">></span>
etcd-master1                               <span class="token number">1</span>/1     Running   <span class="token number">0</span>          8d      <span class="token number">10.10</span>.30.180    master1   <span class="token operator">&lt;</span>none<span class="token operator">></span>           <span class="token operator">&lt;</span>none<span class="token operator">></span>
kube-apiserver-master1                     <span class="token number">1</span>/1     Running   <span class="token number">0</span>          8d      <span class="token number">10.10</span>.30.180    master1   <span class="token operator">&lt;</span>none<span class="token operator">></span>           <span class="token operator">&lt;</span>none<span class="token operator">></span>
kube-controller-manager-master1            <span class="token number">1</span>/1     Running   <span class="token number">0</span>          8d      <span class="token number">10.10</span>.30.180    master1   <span class="token operator">&lt;</span>none<span class="token operator">></span>           <span class="token operator">&lt;</span>none<span class="token operator">></span>
kube-proxy-7vjk5                           <span class="token number">1</span>/1     Running   <span class="token number">0</span>          7d23h   <span class="token number">10.10</span>.30.182    node2     <span class="token operator">&lt;</span>none<span class="token operator">></span>           <span class="token operator">&lt;</span>none<span class="token operator">></span>
kube-proxy-mfpcg                           <span class="token number">1</span>/1     Running   <span class="token number">0</span>          8d      <span class="token number">10.10</span>.30.180    master1   <span class="token operator">&lt;</span>none<span class="token operator">></span>           <span class="token operator">&lt;</span>none<span class="token operator">></span>
kube-proxy-pv87q                           <span class="token number">1</span>/1     Running   <span class="token number">0</span>          7d23h   <span class="token number">10.10</span>.30.181    node1     <span class="token operator">&lt;</span>none<span class="token operator">></span>           <span class="token operator">&lt;</span>none<span class="token operator">></span>
kube-scheduler-master1                     <span class="token number">1</span>/1     Running   <span class="token number">0</span>          8d      <span class="token number">10.10</span>.30.180    master1   <span class="token operator">&lt;</span>none<span class="token operator">></span>           <span class="token operator">&lt;</span>none<span class="token operator">></span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>

<p>我们现在查看一下master节点的污点容忍度</p>
<pre class="line-numbers language-bash" data-language="bash"><code class="language-bash"><span class="token punctuation">[</span>root@master1 ~<span class="token punctuation">]</span><span class="token comment"># kubectl describe nodes master1|grep Taint</span>
Taints:             node-role.kubernetes.io/control-plane:NoSchedule<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span></span></code></pre>

<p>上面可以看到master这个节点的污点是Noschedule，所以我们创建的pod都不会调度到master上，因为我们创建的pod没有容忍度</p>
<p>我们在来看apiserver这个pod,可以看到这个pod的容忍度是NoExecute，则可以调度到master1上</p>
<pre class="line-numbers language-bash" data-language="bash"><code class="language-bash"><span class="token punctuation">[</span>root@master1 ~<span class="token punctuation">]</span><span class="token comment"># kubectl describe pods kube-apiserver-master1 -n kube-system | grep -i tolerations</span>
Tolerations:       :NoExecute <span class="token assign-left variable">op</span><span class="token operator">=</span>Exists<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span></span></code></pre>

<blockquote>
<p>一个容忍度和一个污点相“匹配”是指它们有一样的键名和效果，并且：</p>
<ul>
<li>如果 <code>operator</code> 是 <code>Exists</code> （此时容忍度不能指定 <code>value</code>）</li>
<li>如果 <code>operator</code> 是 <code>Equal</code> ，则它们的 <code>value</code> 应该相等</li>
<li>如果 <code>operator</code> 不指定，则默认为Equal</li>
</ul>
<blockquote>
<p><strong>注意：</strong></p>
<ul>
<li>如果一个容忍度的 <code>key</code> 为空且operator为 <code>Exists</code>， 表示这个容忍度与任意的key 、value 和 effect 都匹配，即这个容忍度能容忍任意 taint。</li>
<li>如果 <code>effect</code> 为空，则可以与所有键名 <code>key1</code> 的效果相匹配。</li>
</ul>
</blockquote>
</blockquote>
<p>管理节点污点</p>
<p>[root@master1]# kubectl taint –help</p>
<p>例1：把node2当成是生产环境专用的，其他node是测试的</p>
<p>[root@master1 ~]# <code>kubectl taint node node2 node-type=production:NoSchedule</code></p>
<p>给node2打污点，pod如果不能容忍就不会调度过来</p>
<p>编写一个taint文件，内容如下：</p>
<p>[root@master1 ~]# cat pod-taint.yaml </p>
<pre class="line-numbers language-yaml" data-language="yaml"><code class="language-yaml"><span class="token key atrule">apiVersion</span><span class="token punctuation">:</span> v1
<span class="token key atrule">kind</span><span class="token punctuation">:</span> Pod
<span class="token key atrule">metadata</span><span class="token punctuation">:</span>
  <span class="token key atrule">name</span><span class="token punctuation">:</span> pod<span class="token punctuation">-</span>taint
  <span class="token key atrule">namespace</span><span class="token punctuation">:</span> default
  <span class="token key atrule">labels</span><span class="token punctuation">:</span>
    <span class="token key atrule">tomcat</span><span class="token punctuation">:</span> tomcat<span class="token punctuation">-</span>pod
<span class="token key atrule">spec</span><span class="token punctuation">:</span>
  <span class="token key atrule">containers</span><span class="token punctuation">:</span>
  <span class="token punctuation">-</span> <span class="token key atrule">name</span><span class="token punctuation">:</span> taint<span class="token punctuation">-</span>pod
    <span class="token key atrule">ports</span><span class="token punctuation">:</span>
    <span class="token punctuation">-</span> <span class="token key atrule">containerPort</span><span class="token punctuation">:</span> <span class="token number">8080</span>
    <span class="token key atrule">image</span><span class="token punctuation">:</span> docker.io/xianchao/tomcat<span class="token punctuation">-</span>8.5<span class="token punctuation">-</span>jre8<span class="token punctuation">:</span>v1
    <span class="token key atrule">imagePullPolicy</span><span class="token punctuation">:</span> IfNotPresent<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>

<p>[root@master1 ~]# kubectl apply -f pod-taint.yaml</p>
<pre class="line-numbers language-bash" data-language="bash"><code class="language-bash"><span class="token punctuation">[</span>root@master1 ~<span class="token punctuation">]</span><span class="token comment"># kubectl taint node node2 node-type=production:NoSchedule</span>
node/node2 tainted
<span class="token punctuation">[</span>root@master1 ~<span class="token punctuation">]</span><span class="token comment"># kubectl get pods -owide</span>
NAME        READY   STATUS    RESTARTS   AGE   IP               NODE    NOMINATED NODE   READINESS GATES
pod-taint   <span class="token number">1</span>/1     Running   <span class="token number">0</span>          34s   <span class="token number">10.224</span>.166.141   node1   <span class="token operator">&lt;</span>none<span class="token operator">></span>           <span class="token operator">&lt;</span>none<span class="token operator">></span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span></span></code></pre>

<p>可以看到都被调度到node1上了，因为node2这个节点打了污点，而我们在创建pod的时候没有容忍度，所以node2上不会有pod调度上去的</p>
<p>例2：给node1也打上污点</p>
<p>[root@master1 ~]#kubectl taint node node1 node-type&#x3D;dev:NoExecute</p>
<p>[root@master1 ~]#kubectl get pods -o wide </p>
<p>显示如下：因为我们没有给taint-pod定义任何容忍度，另外我们给node1打的污点是NoExecute，所以它会把原节点上运行的pod给驱逐出去</p>
<p><img src="https://raw.githubusercontent.com/miaohsukang/blog-pic/master/image-20221109225952606.png"></p>
<p>上面可以看到已经存在的pod节点都被撵走了</p>
<p>我们现在再创建一个pod，来定义一个容忍度</p>
<p>[root@master1 ~]# cat pod-demo-1.yaml </p>
<pre class="line-numbers language-yaml" data-language="yaml"><code class="language-yaml"><span class="token key atrule">apiVersion</span><span class="token punctuation">:</span> v1
<span class="token key atrule">kind</span><span class="token punctuation">:</span> Pod
<span class="token key atrule">metadata</span><span class="token punctuation">:</span>
  <span class="token key atrule">name</span><span class="token punctuation">:</span> myapp<span class="token punctuation">-</span>deploy
  <span class="token key atrule">namespace</span><span class="token punctuation">:</span> default
  <span class="token key atrule">labels</span><span class="token punctuation">:</span>
    <span class="token key atrule">app</span><span class="token punctuation">:</span> myapp
    <span class="token key atrule">release</span><span class="token punctuation">:</span> canary
<span class="token key atrule">spec</span><span class="token punctuation">:</span>
   <span class="token key atrule">containers</span><span class="token punctuation">:</span>
   <span class="token punctuation">-</span> <span class="token key atrule">name</span><span class="token punctuation">:</span> myapp
     <span class="token key atrule">image</span><span class="token punctuation">:</span> ikubernetes/myapp<span class="token punctuation">:</span>v1
     <span class="token key atrule">imagePullPolicy</span><span class="token punctuation">:</span> IfNotPresent
     <span class="token key atrule">ports</span><span class="token punctuation">:</span>
     <span class="token punctuation">-</span> <span class="token key atrule">name</span><span class="token punctuation">:</span> http
       <span class="token key atrule">containerPort</span><span class="token punctuation">:</span> <span class="token number">80</span>
   <span class="token key atrule">tolerations</span><span class="token punctuation">:</span>
   <span class="token punctuation">-</span> <span class="token key atrule">key</span><span class="token punctuation">:</span> <span class="token string">"node-type"</span>
     <span class="token key atrule">operator</span><span class="token punctuation">:</span> <span class="token string">"Equal"</span>  <span class="token comment"># 类似=，需要完全匹配</span>
     <span class="token key atrule">value</span><span class="token punctuation">:</span> <span class="token string">"production"</span>
     <span class="token key atrule">effect</span><span class="token punctuation">:</span> <span class="token string">"NoExecute"</span>
     <span class="token key atrule">tolerationSeconds</span><span class="token punctuation">:</span> <span class="token number">3600</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>

<p>[root@master1 ~]# kubectl apply -f pod-demo-1.yaml</p>
<pre class="line-numbers language-bash" data-language="bash"><code class="language-bash"><span class="token punctuation">[</span>root@master1 ~<span class="token punctuation">]</span><span class="token comment"># kubectl get pods -owide</span>
NAME           READY   STATUS    RESTARTS   AGE   IP       NODE     NOMINATED NODE   READINESS GATES
myapp-deploy   <span class="token number">0</span>/1     Pending   <span class="token number">0</span>          44s   <span class="token operator">&lt;</span>none<span class="token operator">></span>   <span class="token operator">&lt;</span>none<span class="token operator">></span>   <span class="token operator">&lt;</span>none<span class="token operator">></span>           <span class="token operator">&lt;</span>none<span class="token operator">></span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span></span></code></pre>

<p>还是显示pending，因为我们使用的是equal（等值匹配），所以key和value，effect必须和node节点定义的污点完全匹配才可以，把上面配置effect: “NoExecute”变成effect: “NoSchedule”成；tolerationSeconds: 3600这行去掉</p>
<p>删除这个pod，修改后重新生成</p>
<p>[root@master1 ~]# kubectl delete -f pod-demo-1.yaml</p>
<p>[root@master1 ~]# kubectl apply -f pod-demo-1.yaml</p>
<pre class="line-numbers language-bash" data-language="bash"><code class="language-bash"><span class="token punctuation">[</span>root@master1 ~<span class="token punctuation">]</span><span class="token comment"># kubectl get pods -owide</span>
NAME           READY   STATUS    RESTARTS   AGE   IP              NODE    NOMINATED NODE   READINESS GATES
myapp-deploy   <span class="token number">1</span>/1     Running   <span class="token number">0</span>          4s    <span class="token number">10.224</span>.104.21   node2   <span class="token operator">&lt;</span>none<span class="token operator">></span>           <span class="token operator">&lt;</span>none<span class="token operator">></span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span></span></code></pre>

<p>上面就可以调度到node2上了，因为在pod中定义的容忍度能容忍node节点上的污点</p>
<p>例3：再次修改</p>
<p>修改如下部分：</p>
<pre class="line-numbers language-yaml" data-language="yaml"><code class="language-yaml"><span class="token key atrule">tolerations</span><span class="token punctuation">:</span>
<span class="token punctuation">-</span> <span class="token key atrule">key</span><span class="token punctuation">:</span> <span class="token string">"node-type"</span>
  <span class="token key atrule">operator</span><span class="token punctuation">:</span> <span class="token string">"Exists"</span>
  <span class="token key atrule">value</span><span class="token punctuation">:</span> <span class="token string">""</span>  <span class="token comment"># 可以不写</span>
  <span class="token key atrule">effect</span><span class="token punctuation">:</span> <span class="token string">"NoSchedule"</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span></span></code></pre>

<p>只要对应的键是存在的，exists，其值被自动定义成通配符</p>
<p>[root@master1 ~]# kubectl delete -f pod-demo-1.yaml</p>
<p>[root@master1 ~]# kubectl apply -f pod-demo-1.yaml</p>
<p>发现还是调度到node2上</p>
<pre class="line-numbers language-bash" data-language="bash"><code class="language-bash"><span class="token punctuation">[</span>root@master1 ~<span class="token punctuation">]</span><span class="token comment"># kubectl get pods -owide</span>
NAME           READY   STATUS    RESTARTS   AGE   IP              NODE    NOMINATED NODE   READINESS GATES
myapp-deploy   <span class="token number">1</span>/1     Running   <span class="token number">0</span>          3s    <span class="token number">10.224</span>.104.22   node2   <span class="token operator">&lt;</span>none<span class="token operator">></span>           <span class="token operator">&lt;</span>none<span class="token operator">></span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span></span></code></pre>

<p>再次修改：</p>
<pre class="line-numbers language-yaml" data-language="yaml"><code class="language-yaml"><span class="token key atrule">tolerations</span><span class="token punctuation">:</span>
<span class="token punctuation">-</span> <span class="token key atrule">key</span><span class="token punctuation">:</span> <span class="token string">"node-type"</span>
  <span class="token key atrule">operator</span><span class="token punctuation">:</span> <span class="token string">"Exists"</span>
  <span class="token key atrule">value</span><span class="token punctuation">:</span> <span class="token string">""</span>   <span class="token comment"># 可以不写</span>
  <span class="token key atrule">effect</span><span class="token punctuation">:</span> <span class="token string">""</span>  <span class="token comment"># 可以不写</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span></span></code></pre>

<p>有一个node-type的键，不管值是什么，不管是什么效果，都能容忍</p>
<p>[root@master1 ~]# kubectl delete -f pod-demo-1.yaml</p>
<p>[root@master1 ~]# kubectl apply -f pod-demo-1.yaml</p>
<pre class="line-numbers language-bash" data-language="bash"><code class="language-bash"><span class="token punctuation">[</span>root@master1 ~<span class="token punctuation">]</span><span class="token comment"># kubectl get pods -owide</span>
NAME           READY   STATUS    RESTARTS   AGE   IP               NODE    NOMINATED NODE   READINESS GATES
myapp-deploy   <span class="token number">1</span>/1     Running   <span class="token number">0</span>          4s    <span class="token number">10.224</span>.166.142   node1   <span class="token operator">&lt;</span>none<span class="token operator">></span>           <span class="token operator">&lt;</span>none<span class="token operator">></span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span></span></code></pre>

<p>可以看到node2和node1节点上都有可能有pod被调度</p>
<p>删除污点：</p>
<p>[root@master1 taint]# <code>kubectl taint nodes node1 node-type:NoExecute-</code></p>
<p>[root@master1 taint]# <code>kubectl taint nodes node2 node-type-</code></p>
<h3 id="五、Pod常见的状态和重启策略"><a href="#五、Pod常见的状态和重启策略" class="headerlink" title="五、Pod常见的状态和重启策略"></a>五、Pod常见的状态和重启策略</h3><h4 id="5-1-常见的pod状态"><a href="#5-1-常见的pod状态" class="headerlink" title="5.1 常见的pod状态"></a>5.1 常见的pod状态</h4><p><img src="https://raw.githubusercontent.com/miaohsukang/blog-pic/master/wps2-1667910193800-2.jpg"> </p>
<p>pod常见的状态：</p>
<p><strong>第一阶段：</strong></p>
<p>挂起（Pending）：</p>
<p>​	1、正在创建Pod但是Pod中的容器还没有全部被创建完成，处于此状态的Pod应该检查Pod依赖的存储是否有权限挂载、镜像是否可以下载、调度是否正常等</p>
<p>​	2、我们在请求创建pod时，条件不满足，调度没有完成，没有任何一个节点能满足调度条件，已经创建了pod但是没有适合它运行的节点叫做挂起，调度没有完成。 </p>
<p>失败（Failed）：Pod 中的所有容器都已终止了，并且至少有一个容器是因为失败终止。也就是说，容器以非0状态退出或者被系统终止。</p>
<p>未知（Unknown）：未知状态，所谓pod是什么状态是apiserver和运行在pod节点的kubelet进行通信获取状态信息的，如果节点之上的kubelet本身出故障，那么apiserver就连不上kubelet，得不到信息了，就会看Unknown，通常是由于与pod所在的node节点通信错误。</p>
<p>Error 状态：Pod 启动过程中发生了错误</p>
<p>成功（Succeeded）：Pod中的所有容器都被成功终止，即pod里所有的containers均已terminated。 </p>
<p><strong>第二阶段：</strong></p>
<p>Unschedulable：Pod不能被调度， scheduler没有匹配到合适的node节点</p>
<p>PodScheduled：pod正处于调度中，在scheduler刚开始调度的时候，还没有将pod分配到指定的node，在筛选出合适的节点后就会更新etcd数据，将pod分配到指定的node</p>
<p>Initialized：所有pod中的初始化容器已经完成了</p>
<p>ImagePullBackOff：Pod所在的node节点下载镜像失败</p>
<p>Running：Pod内部的容器已经被创建并且启动。</p>
<p>扩展：还有其他状态，如下：</p>
<p>Evicted状态：出现这种情况，多见于系统内存或硬盘资源不足，可df-h查看docker存储所在目录的资源使用情况，如果百分比大于85%，就要及时清理下资源，尤其是一些大文件、docker镜像。</p>
<p>CrashLoopBackOff：容器曾经启动了，但可能又异常退出了</p>
<h4 id="5-2-pod重启策略"><a href="#5-2-pod重启策略" class="headerlink" title="5.2 pod重启策略"></a>5.2 pod重启策略</h4><p>Pod的重启策略（RestartPolicy）应用于Pod内的所有容器，当某个容器异常退出或者健康检查失败时，kubelet将根据重启策略来进行相应的操作。</p>
<p>Pod的spec中包含一个restartPolicy字段，其可能取值包括Always、OnFailure 和Never。默认值是Always。</p>
<ul>
<li><p>Always：只要容器异常退出，kubelet就会自动重启该容器。（这个是默认的重启策略）</p>
</li>
<li><p>OnFailure：当容器终止运行且退出码不为0时，由kubelet自动重启该容器。</p>
</li>
<li><p>Never：不论容器运行状态如何，kubelet都不会重启该容器。</p>
</li>
</ul>
<h4 id="5-3-测试Always重启策略"><a href="#5-3-测试Always重启策略" class="headerlink" title="5.3 测试Always重启策略"></a>5.3 测试Always重启策略</h4><p>[root@master1]# cat pod.yaml </p>
<pre class="line-numbers language-yaml" data-language="yaml"><code class="language-yaml"><span class="token key atrule">apiVersion</span><span class="token punctuation">:</span> v1
<span class="token key atrule">kind</span><span class="token punctuation">:</span> Pod
<span class="token key atrule">metadata</span><span class="token punctuation">:</span>
  <span class="token key atrule">name</span><span class="token punctuation">:</span> demo<span class="token punctuation">-</span>pod
  <span class="token key atrule">namespace</span><span class="token punctuation">:</span> default
  <span class="token key atrule">labels</span><span class="token punctuation">:</span>
    <span class="token key atrule">app</span><span class="token punctuation">:</span> myapp
<span class="token key atrule">spec</span><span class="token punctuation">:</span>
  <span class="token key atrule">restartPolicy</span><span class="token punctuation">:</span> Always
  <span class="token key atrule">containers</span><span class="token punctuation">:</span>
  <span class="token punctuation">-</span> <span class="token key atrule">name</span><span class="token punctuation">:</span>  tomcat<span class="token punctuation">-</span>pod<span class="token punctuation">-</span>java
    <span class="token key atrule">ports</span><span class="token punctuation">:</span>
    <span class="token punctuation">-</span> <span class="token key atrule">containerPort</span><span class="token punctuation">:</span> <span class="token number">8080</span>
    <span class="token key atrule">image</span><span class="token punctuation">:</span> xianchao/tomcat<span class="token punctuation">-</span>8.5<span class="token punctuation">-</span>jre8<span class="token punctuation">:</span>v1
    <span class="token key atrule">imagePullPolicy</span><span class="token punctuation">:</span> IfNotPresent<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>

<p>[root@master1]# kubectl apply -f pod.yaml</p>
<p>1）正常停止容器里的tomcat服务</p>
<pre class="line-numbers language-bash" data-language="bash"><code class="language-bash"><span class="token punctuation">[</span>root@master1 ~<span class="token punctuation">]</span><span class="token comment"># kubectl exec -it demo-pod -- /bin/bash</span>
bash-4.4<span class="token comment"># /usr/local/tomcat/bin/shutdown.sh</span>
Using CATALINA_BASE:   /usr/local/tomcat
Using CATALINA_HOME:   /usr/local/tomcat
Using CATALINA_TMPDIR: /usr/local/tomcat/temp
Using JRE_HOME:        /usr/lib/jvm/java-1.8-openjdk/jre
Using CLASSPATH:       /usr/local/tomcat/bin/bootstrap.jar:/usr/local/tomcat/bin/tomcat-juli.jar
bash-4.4<span class="token comment"># command terminated with exit code 137</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>

<p>查看pod状态：</p>
<pre class="line-numbers language-bash" data-language="bash"><code class="language-bash"><span class="token punctuation">[</span>root@master1 pod<span class="token punctuation">]</span><span class="token comment"># kubectl get pods -w</span>
NAME       READY   STATUS    RESTARTS     AGE
demo-pod   <span class="token number">1</span>/1     Running   <span class="token number">1</span> <span class="token punctuation">(</span>4s ago<span class="token punctuation">)</span>   79s
demo-pod   <span class="token number">0</span>/1     Completed   <span class="token number">1</span> <span class="token punctuation">(</span>35s ago<span class="token punctuation">)</span>   110s
demo-pod   <span class="token number">0</span>/1     CrashLoopBackOff   <span class="token number">1</span> <span class="token punctuation">(</span>15s ago<span class="token punctuation">)</span>   2m5s
demo-pod   <span class="token number">1</span>/1     Running            <span class="token number">2</span> <span class="token punctuation">(</span>16s ago<span class="token punctuation">)</span>   2m6s<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>

<p>发现正常停止容器里的tomcat服务，容器重启了一次，pod又恢复正常了</p>
<p>2）非正常停止容器里的tomcat服务</p>
<pre class="line-numbers language-bash" data-language="bash"><code class="language-bash"><span class="token punctuation">[</span>root@master1 ~<span class="token punctuation">]</span><span class="token comment"># kubectl exec -it demo-pod -- /bin/bash</span>
bash-4.4<span class="token comment"># kill 1</span>
bash-4.4<span class="token comment"># command terminated with exit code 137</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span></span></code></pre>

<p>这个时候查看pod运行状态</p>
 <pre class="line-numbers language-bash" data-language="bash"><code class="language-bash"><span class="token punctuation">[</span>root@master1 pod<span class="token punctuation">]</span><span class="token comment"># kubectl get pods -w</span>
NAME       READY   STATUS    RESTARTS        AGE
demo-pod   <span class="token number">1</span>/1     Running   <span class="token number">2</span> <span class="token punctuation">(</span>2m25s ago<span class="token punctuation">)</span>   4m15s
demo-pod   <span class="token number">0</span>/1     Error     <span class="token number">2</span> <span class="token punctuation">(</span>2m37s ago<span class="token punctuation">)</span>   4m27s
demo-pod   <span class="token number">0</span>/1     CrashLoopBackOff   <span class="token number">2</span> <span class="token punctuation">(</span>15s ago<span class="token punctuation">)</span>     4m42s
demo-pod   <span class="token number">1</span>/1     Running            <span class="token number">3</span> <span class="token punctuation">(</span>29s ago<span class="token punctuation">)</span>     4m56s<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>

<p>上面可以看到容器终止了，并且又重启一次，重启次数增加了一次</p>
<h4 id="5-4-测试never重启策略"><a href="#5-4-测试never重启策略" class="headerlink" title="5.4 测试never重启策略"></a>5.4 测试never重启策略</h4><p>[root@master1 ~]# vim pod.yaml </p>
<pre class="line-numbers language-yaml" data-language="yaml"><code class="language-yaml"><span class="token key atrule">apiVersion</span><span class="token punctuation">:</span> v1
<span class="token key atrule">kind</span><span class="token punctuation">:</span> Pod
<span class="token key atrule">metadata</span><span class="token punctuation">:</span>
  <span class="token key atrule">name</span><span class="token punctuation">:</span> demo<span class="token punctuation">-</span>pod
  <span class="token key atrule">namespace</span><span class="token punctuation">:</span> default
  <span class="token key atrule">labels</span><span class="token punctuation">:</span>
    <span class="token key atrule">app</span><span class="token punctuation">:</span> myapp
<span class="token key atrule">spec</span><span class="token punctuation">:</span>
  <span class="token key atrule">restartPolicy</span><span class="token punctuation">:</span> Never
  <span class="token key atrule">containers</span><span class="token punctuation">:</span>
  <span class="token punctuation">-</span> <span class="token key atrule">name</span><span class="token punctuation">:</span>  tomcat<span class="token punctuation">-</span>pod<span class="token punctuation">-</span>java
    <span class="token key atrule">ports</span><span class="token punctuation">:</span>
    <span class="token punctuation">-</span> <span class="token key atrule">containerPort</span><span class="token punctuation">:</span> <span class="token number">8080</span>
    <span class="token key atrule">image</span><span class="token punctuation">:</span> xianchao/tomcat<span class="token punctuation">-</span>8.5<span class="token punctuation">-</span>jre8<span class="token punctuation">:</span>v1
    <span class="token key atrule">imagePullPolicy</span><span class="token punctuation">:</span> IfNotPresent<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>

<p>[root@master1]# kubectl apply -f pod.yaml</p>
<p>1）正常停止容器里的tomcat服务</p>
<pre class="line-numbers language-bash" data-language="bash"><code class="language-bash"><span class="token punctuation">[</span>root@master1 ~<span class="token punctuation">]</span><span class="token comment"># kubectl exec -it demo-pod -- /bin/bash</span>
bash-4.4<span class="token comment"># /usr/local/tomcat/bin/shutdown.sh</span>
Using CATALINA_BASE:   /usr/local/tomcat
Using CATALINA_HOME:   /usr/local/tomcat
Using CATALINA_TMPDIR: /usr/local/tomcat/temp
Using JRE_HOME:        /usr/lib/jvm/java-1.8-openjdk/jre
Using CLASSPATH:       /usr/local/tomcat/bin/bootstrap.jar:/usr/local/tomcat/bin/tomcat-juli.jar
bash-4.4<span class="token comment"># command terminated with exit code 137</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>

<p>查看pod状态：</p>
<pre class="line-numbers language-bash" data-language="bash"><code class="language-bash"><span class="token punctuation">[</span>root@master1 pod<span class="token punctuation">]</span><span class="token comment"># kubectl get pods -w</span>
NAME       READY   STATUS    RESTARTS   AGE
demo-pod   <span class="token number">1</span>/1     Running   <span class="token number">0</span>          16s
demo-pod   <span class="token number">0</span>/1     Completed   <span class="token number">0</span>          20s
demo-pod   <span class="token number">0</span>/1     Completed   <span class="token number">0</span>          21s
demo-pod   <span class="token number">0</span>/1     Completed   <span class="token number">0</span>          22s<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>

<p>发现正常停止容器里的tomcat服务，pod正常运行，容器没有重启</p>
<p>2）非正常停止容器里的tomcat服务</p>
<pre class="line-numbers language-bash" data-language="bash"><code class="language-bash"><span class="token punctuation">[</span>root@master1 ~<span class="token punctuation">]</span><span class="token comment"># kubectl exec -it demo-pod -- /bin/bash</span>
bash-4.4<span class="token comment"># kill 1</span>
bash-4.4<span class="token comment"># command terminated with exit code 137</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span></span></code></pre>

<p>再次查看pod状态</p>
<pre class="line-numbers language-bash" data-language="bash"><code class="language-bash"><span class="token punctuation">[</span>root@master1 pod<span class="token punctuation">]</span><span class="token comment"># kubectl get pods -w</span>
NAME       READY   STATUS    RESTARTS   AGE
demo-pod   <span class="token number">1</span>/1     Running   <span class="token number">0</span>          4s
demo-pod   <span class="token number">0</span>/1     Error     <span class="token number">0</span>          15s
demo-pod   <span class="token number">0</span>/1     Error     <span class="token number">0</span>          16s
demo-pod   <span class="token number">0</span>/1     Error     <span class="token number">0</span>          17s<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>

<p>上面可以看到容器状态是error，并且没有重启，这说明重启策略是never，那么pod里容器服务无论如何终止，都不会重启</p>
<h4 id="5-5-测试OnFailure重启策略"><a href="#5-5-测试OnFailure重启策略" class="headerlink" title="5.5 测试OnFailure重启策略"></a>5.5 测试OnFailure重启策略</h4><p>[root@master1 ~]# vim pod.yaml </p>
<pre class="line-numbers language-yaml" data-language="yaml"><code class="language-yaml"><span class="token key atrule">apiVersion</span><span class="token punctuation">:</span> v1
<span class="token key atrule">kind</span><span class="token punctuation">:</span> Pod
<span class="token key atrule">metadata</span><span class="token punctuation">:</span>
  <span class="token key atrule">name</span><span class="token punctuation">:</span> demo<span class="token punctuation">-</span>pod
  <span class="token key atrule">namespace</span><span class="token punctuation">:</span> default
  <span class="token key atrule">labels</span><span class="token punctuation">:</span>
    <span class="token key atrule">app</span><span class="token punctuation">:</span> myapp
<span class="token key atrule">spec</span><span class="token punctuation">:</span>
  <span class="token key atrule">restartPolicy</span><span class="token punctuation">:</span> OnFailure
  <span class="token key atrule">containers</span><span class="token punctuation">:</span>
  <span class="token punctuation">-</span> <span class="token key atrule">name</span><span class="token punctuation">:</span>  tomcat<span class="token punctuation">-</span>pod<span class="token punctuation">-</span>java
    <span class="token key atrule">ports</span><span class="token punctuation">:</span>
    <span class="token punctuation">-</span> <span class="token key atrule">containerPort</span><span class="token punctuation">:</span> <span class="token number">8080</span>
    <span class="token key atrule">image</span><span class="token punctuation">:</span> xianchao/tomcat<span class="token punctuation">-</span>8.5<span class="token punctuation">-</span>jre8<span class="token punctuation">:</span>v1
    <span class="token key atrule">imagePullPolicy</span><span class="token punctuation">:</span> IfNotPresent<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>

<p>[root@master1]# kubectl apply -f pod.yaml</p>
<p>1）正常停止容器里的tomcat服务</p>
<pre class="line-numbers language-bash" data-language="bash"><code class="language-bash"><span class="token punctuation">[</span>root@master1 ~<span class="token punctuation">]</span><span class="token comment"># kubectl exec -it demo-pod -- /bin/bash</span>
bash-4.4<span class="token comment"># /usr/local/tomcat/bin/shutdown.sh</span>
Using CATALINA_BASE:   /usr/local/tomcat
Using CATALINA_HOME:   /usr/local/tomcat
Using CATALINA_TMPDIR: /usr/local/tomcat/temp
Using JRE_HOME:        /usr/lib/jvm/java-1.8-openjdk/jre
Using CLASSPATH:       /usr/local/tomcat/bin/bootstrap.jar:/usr/local/tomcat/bin/tomcat-juli.jar
bash-4.4<span class="token comment"># command terminated with exit code 137</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>

<p>查看pod状态：</p>
<pre class="line-numbers language-bash" data-language="bash"><code class="language-bash"><span class="token punctuation">[</span>root@master1 pod<span class="token punctuation">]</span><span class="token comment"># kubectl get pods -w</span>
NAME       READY   STATUS    RESTARTS   AGE
demo-pod   <span class="token number">1</span>/1     Running   <span class="token number">0</span>          4s
demo-pod   <span class="token number">0</span>/1     Completed   <span class="token number">0</span>          12s
demo-pod   <span class="token number">0</span>/1     Completed   <span class="token number">0</span>          14s<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span></span></code></pre>

<p>发现正常停止容器里的tomcat服务，退出码是0，pod里的容器不会重启</p>
<p>2）非正常停止容器里的tomcat服务</p>
<pre class="line-numbers language-bash" data-language="bash"><code class="language-bash"><span class="token punctuation">[</span>root@master1 ~<span class="token punctuation">]</span><span class="token comment"># kubectl exec -it demo-pod -- /bin/bash</span>
bash-4.4<span class="token comment"># kill 1</span>
bash-4.4<span class="token comment"># command terminated with exit code 137</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span></span></code></pre>

<p>查看pod状态</p>
<pre class="line-numbers language-bash" data-language="bash"><code class="language-bash"><span class="token punctuation">[</span>root@master1 pod<span class="token punctuation">]</span><span class="token comment"># kubectl get pods -w</span>
NAME       READY   STATUS    RESTARTS   AGE
demo-pod   <span class="token number">1</span>/1     Running   <span class="token number">0</span>          2s
demo-pod   <span class="token number">0</span>/1     Error     <span class="token number">0</span>          9s
demo-pod   <span class="token number">1</span>/1     Running   <span class="token number">1</span> <span class="token punctuation">(</span>2s ago<span class="token punctuation">)</span>   10s
demo-pod   <span class="token number">1</span>/1     Running   <span class="token number">1</span> <span class="token punctuation">(</span>25s ago<span class="token punctuation">)</span>   33s<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>

<p>上面可以看到非正常停止pod里的容器，容器退出码不是0，那就会重启容器</p>

                
            </div>
            <hr/>

            

    <div class="reprint" id="reprint-statement">
        
            <div class="reprint__author">
                <span class="reprint-meta" style="font-weight: bold;">
                    <i class="fas fa-user">
                        文章作者:
                    </i>
                </span>
                <span class="reprint-info">
                    <a href="/about" rel="external nofollow noreferrer">miaohsukang</a>
                </span>
            </div>
            <div class="reprint__type">
                <span class="reprint-meta" style="font-weight: bold;">
                    <i class="fas fa-link">
                        文章链接:
                    </i>
                </span>
                <span class="reprint-info">
                    <a href="https://miaohsukang.gitee.io/2023/02/14/k8s-bi-ji/di-4-zhang-biao-qian-qin-he-xing-wu-dian-rong-ren-du/">https://miaohsukang.gitee.io/2023/02/14/k8s-bi-ji/di-4-zhang-biao-qian-qin-he-xing-wu-dian-rong-ren-du/</a>
                </span>
            </div>
            <div class="reprint__notice">
                <span class="reprint-meta" style="font-weight: bold;">
                    <i class="fas fa-copyright">
                        版权声明:
                    </i>
                </span>
                <span class="reprint-info">
                    本博客所有文章除特別声明外，均采用
                    <a href="https://creativecommons.org/licenses/by/4.0/deed.zh" rel="external nofollow noreferrer" target="_blank">CC BY 4.0</a>
                    许可协议。转载请注明来源
                    <a href="/about" target="_blank">miaohsukang</a>
                    !
                </span>
            </div>
        
    </div>

    <script async defer>
      document.addEventListener("copy", function (e) {
        let toastHTML = '<span>复制成功，请遵循本文的转载规则</span><button class="btn-flat toast-action" onclick="navToReprintStatement()" style="font-size: smaller">查看</a>';
        M.toast({html: toastHTML})
      });

      function navToReprintStatement() {
        $("html, body").animate({scrollTop: $("#reprint-statement").offset().top - 80}, 800);
      }
    </script>



            <div class="tag_share" style="display: block;">
                <div class="post-meta__tag-list" style="display: inline-block;">
                    
                        <div class="article-tag">
                            
                                <a href="/tags/k8s%E6%A0%87%E7%AD%BE/">
                                    <span class="chip bg-color">k8s标签</span>
                                </a>
                            
                                <a href="/tags/k8s%E4%BA%B2%E5%92%8C%E6%80%A7/">
                                    <span class="chip bg-color">k8s亲和性</span>
                                </a>
                            
                        </div>
                    
                </div>
                <div class="post_share" style="zoom: 80%; width: fit-content; display: inline-block; float: right; margin: -0.15rem 0;">
                    <link rel="stylesheet" type="text/css" href="/libs/share/css/share.min.css">
<div id="article-share">

    
    <div class="social-share" data-sites="twitter,facebook,google,qq,qzone,wechat,weibo,douban,linkedin" data-wechat-qrcode-helper="<p>微信扫一扫即可分享！</p>"></div>
    <script src="/libs/share/js/social-share.min.js"></script>
    

    

</div>

                </div>
            </div>
            
                <div id="reward">
    <a href="#rewardModal" class="reward-link modal-trigger btn-floating btn-medium waves-effect waves-light red">赏</a>

    <!-- Modal Structure -->
    <div id="rewardModal" class="modal">
        <div class="modal-content">
            <a class="close modal-close"><i class="fas fa-times"></i></a>
            <h4 class="reward-title">你的赏识是我前进的动力</h4>
            <div class="reward-content">
                <div class="reward-tabs">
                    <ul class="tabs row">
                        <li class="tab col s6 alipay-tab waves-effect waves-light"><a href="#alipay">支付宝</a></li>
                        <li class="tab col s6 wechat-tab waves-effect waves-light"><a href="#wechat">微 信</a></li>
                    </ul>
                    <div id="alipay">
                        <img src="/medias/reward/alipay.jpg" class="reward-img" alt="支付宝打赏二维码">
                    </div>
                    <div id="wechat">
                        <img src="/medias/reward/wechat.png" class="reward-img" alt="微信打赏二维码">
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>

<script>
    $(function () {
        $('.tabs').tabs();
    });
</script>

            
        </div>
    </div>

    

    

    

    

    

    

    

    

    

<article id="prenext-posts" class="prev-next articles">
    <div class="row article-row">
        
        <div class="article col s12 m6" data-aos="fade-up" data-aos="fade-up">
            <div class="article-badge left-badge text-color">
                <i class="far fa-dot-circle"></i>&nbsp;本篇
            </div>
            <div class="card">
                <a href="/2023/02/14/k8s-bi-ji/di-4-zhang-biao-qian-qin-he-xing-wu-dian-rong-ren-du/">
                    <div class="card-image">
                        
                        <img src="https://raw.githubusercontent.com/miaohsukang/blog-pic/master/202302142203068.jpg" class="responsive-img" alt="第4章-标签亲和性污点容忍度">
                        
                        <span class="card-title">第4章-标签亲和性污点容忍度</span>
                    </div>
                </a>
                <div class="card-content article-content">
                    <div class="summary block-with-text">
                        
                            
                        
                    </div>
                    <div class="publish-info">
                            <span class="publish-date">
                                <i class="far fa-clock fa-fw icon-date"></i>2023-02-14
                            </span>
                        <span class="publish-author">
                            
                            <i class="fas fa-bookmark fa-fw icon-category"></i>
                            
                            <a href="/categories/K8S%E7%AC%94%E8%AE%B0/" class="post-category">
                                    K8S笔记
                                </a>
                            
                            
                        </span>
                    </div>
                </div>

                
                <div class="card-action article-tags">
                    
                    <a href="/tags/k8s%E6%A0%87%E7%AD%BE/">
                        <span class="chip bg-color">k8s标签</span>
                    </a>
                    
                    <a href="/tags/k8s%E4%BA%B2%E5%92%8C%E6%80%A7/">
                        <span class="chip bg-color">k8s亲和性</span>
                    </a>
                    
                </div>
                
            </div>
        </div>
        
        
        <div class="article col s12 m6" data-aos="fade-up">
            <div class="article-badge right-badge text-color">
                下一篇&nbsp;<i class="fas fa-chevron-right"></i>
            </div>
            <div class="card">
                <a href="/2023/02/12/k8s-bi-ji/di-2-zhang-pod-gai-shu-md/">
                    <div class="card-image">
                        
                        <img src="https://www.zutuanxue.com:8000/static/media/images/2020/10/10/1602326898782.png" class="responsive-img" alt="第2章-pod概述.md">
                        
                        <span class="card-title">第2章-pod概述.md</span>
                    </div>
                </a>
                <div class="card-content article-content">
                    <div class="summary block-with-text">
                        
                            
                        
                    </div>
                    <div class="publish-info">
                            <span class="publish-date">
                                <i class="far fa-clock fa-fw icon-date"></i>2023-02-12
                            </span>
                        <span class="publish-author">
                            
                            <i class="fas fa-bookmark fa-fw icon-category"></i>
                            
                            <a href="/categories/K8S%E7%AC%94%E8%AE%B0/" class="post-category">
                                    K8S笔记
                                </a>
                            
                            
                        </span>
                    </div>
                </div>
                
                <div class="card-action article-tags">
                    
                    <a href="/tags/pod/">
                        <span class="chip bg-color">pod</span>
                    </a>
                    
                </div>
                
            </div>
        </div>
        
    </div>
</article>

</div>



<!-- 代码块功能依赖 -->
<script type="text/javascript" src="/libs/codeBlock/codeBlockFuction.js"></script>


  <!-- 是否加载使用自带的 prismjs. -->
  <script type="text/javascript" src="/libs/prism/prism.min.js"></script>


<!-- 代码语言 -->

<script type="text/javascript" src="/libs/codeBlock/codeLang.js"></script>


<!-- 代码块复制 -->

<script type="text/javascript" src="/libs/codeBlock/codeCopy.js"></script>


<!-- 代码块收缩 -->

<script type="text/javascript" src="/libs/codeBlock/codeShrink.js"></script>



    </div>
    <div id="toc-aside" class="expanded col l3 hide-on-med-and-down">
        <div class="toc-widget card" style="background-color: white;">
            <div class="toc-title"><i class="far fa-list-alt"></i>&nbsp;&nbsp;目录</div>
            <div id="toc-content"></div>
        </div>
    </div>
</div>

<!-- TOC 悬浮按钮. -->


<script src="/libs/tocbot/tocbot.min.js"></script>
<script>
    $(function () {
        tocbot.init({
            tocSelector: '#toc-content',
            contentSelector: '#articleContent',
            headingsOffset: -($(window).height() * 0.4 - 45),
            collapseDepth: Number('0'),
            headingSelector: 'h2, h3, h4'
        });

        // Set scroll toc fixed.
        let tocHeight = parseInt($(window).height() * 0.4 - 64);
        let $tocWidget = $('.toc-widget');
        $(window).scroll(function () {
            let scroll = $(window).scrollTop();
            /* add post toc fixed. */
            if (scroll > tocHeight) {
                $tocWidget.addClass('toc-fixed');
            } else {
                $tocWidget.removeClass('toc-fixed');
            }
        });

        
    });
</script>

    

</main>




    <footer class="page-footer bg-color">
    
        <link rel="stylesheet" href="/libs/aplayer/APlayer.min.css">
<style>
    .aplayer .aplayer-lrc p {
        
        display: none;
        
        font-size: 12px;
        font-weight: 700;
        line-height: 16px !important;
    }

    .aplayer .aplayer-lrc p.aplayer-lrc-current {
        
        display: none;
        
        font-size: 15px;
        color: #42b983;
    }

    
    .aplayer.aplayer-fixed.aplayer-narrow .aplayer-body {
        left: -66px !important;
    }

    .aplayer.aplayer-fixed.aplayer-narrow .aplayer-body:hover {
        left: 0px !important;
    }

    
</style>
<div class="">
    
    <div class="row">
        <meting-js class="col l8 offset-l2 m10 offset-m1 s12"
                   server="netease"
                   type="playlist"
                   id="503838841"
                   fixed='true'
                   autoplay='false'
                   theme='#42b983'
                   loop='all'
                   order='random'
                   preload='auto'
                   volume='0.7'
                   list-folded='true'
        >
        </meting-js>
    </div>
</div>

<script src="/libs/aplayer/APlayer.min.js"></script>
<script src="/libs/aplayer/Meting.min.js"></script>

    

    <div class="container row center-align"
         style="margin-bottom: 0px !important;">
        <div class="col s12 m8 l8 copy-right">
            Copyright&nbsp;&copy;
            
                <span id="year">2023</span>
            
            <a href="/about" target="_blank">Miaohsukang</a>
            |&nbsp;Powered by&nbsp;<a href="https://hexo.io/" target="_blank">Hexo</a>
            |&nbsp;Theme&nbsp;<a href="https://miaohsukang.gitee.io" target="_blank">Matery</a>
            
            <br>
            
            
            
                
            
            
                <span id="busuanzi_container_site_pv">
                &nbsp;|&nbsp;<i class="far fa-eye"></i>&nbsp;总访问量:&nbsp;
                    <span id="busuanzi_value_site_pv" class="white-color"></span>
            </span>
            
            
                <span id="busuanzi_container_site_uv">
                &nbsp;|&nbsp;<i class="fas fa-users"></i>&nbsp;总访问人数:&nbsp;
                    <span id="busuanzi_value_site_uv" class="white-color"></span>
            </span>
            
            <br>

            <!-- 运行天数提醒. -->
            
            <br>
            
        </div>
        <div class="col s12 m4 l4 social-link social-statis">
    <a href="https://miaohsukang.gitee.io" class="tooltipped" target="_blank" data-tooltip="访问我的GitHub" data-position="top" data-delay="50">
        <i class="fab fa-github"></i>
    </a>



    <a href="mailto:12419315+miaohsukang@user.noreply.gitee.com" class="tooltipped" target="_blank" data-tooltip="邮件联系我" data-position="top" data-delay="50">
        <i class="fas fa-envelope-open"></i>
    </a>



    <a href="tencent://AddContact/?fromId=50&fromSubId=1&subcmd=all&uin=258603904" class="tooltipped" target="_blank" data-tooltip="QQ联系我: 258603904" data-position="top" data-delay="50">
        <i class="fab fa-qq"></i>
    </a>

</div>
    </div>
</footer>

<div class="progress-bar"></div>


    <!-- 搜索遮罩框 -->
<div id="searchModal" class="modal">
    <div class="modal-content">
        <div class="search-header">
            <span class="title"><i class="fas fa-search"></i>&nbsp;&nbsp;搜索</span>
            <input type="search" id="searchInput" name="s" placeholder="请输入搜索的关键字"
                   class="search-input">
        </div>
        <div id="searchResult"></div>
    </div>
</div>

<script type="text/javascript">
$(function () {
    var searchFunc = function (path, search_id, content_id) {
        'use strict';
        $.ajax({
            url: path,
            dataType: "xml",
            success: function (xmlResponse) {
                // get the contents from search data
                var datas = $("entry", xmlResponse).map(function () {
                    return {
                        title: $("title", this).text(),
                        content: $("content", this).text(),
                        url: $("url", this).text()
                    };
                }).get();
                var $input = document.getElementById(search_id);
                var $resultContent = document.getElementById(content_id);
                $input.addEventListener('input', function () {
                    var str = '<ul class=\"search-result-list\">';
                    var keywords = this.value.trim().toLowerCase().split(/[\s\-]+/);
                    $resultContent.innerHTML = "";
                    if (this.value.trim().length <= 0) {
                        return;
                    }
                    // perform local searching
                    datas.forEach(function (data) {
                        var isMatch = true;
                        var data_title = data.title.trim().toLowerCase();
                        var data_content = data.content.trim().replace(/<[^>]+>/g, "").toLowerCase();
                        var data_url = data.url;
                        data_url = data_url.indexOf('/') === 0 ? data.url : '/' + data_url;
                        var index_title = -1;
                        var index_content = -1;
                        var first_occur = -1;
                        // only match artiles with not empty titles and contents
                        if (data_title !== '' && data_content !== '') {
                            keywords.forEach(function (keyword, i) {
                                index_title = data_title.indexOf(keyword);
                                index_content = data_content.indexOf(keyword);
                                if (index_title < 0 && index_content < 0) {
                                    isMatch = false;
                                } else {
                                    if (index_content < 0) {
                                        index_content = 0;
                                    }
                                    if (i === 0) {
                                        first_occur = index_content;
                                    }
                                }
                            });
                        }
                        // show search results
                        if (isMatch) {
                            str += "<li><a href='" + data_url + "' class='search-result-title'>" + data_title + "</a>";
                            var content = data.content.trim().replace(/<[^>]+>/g, "");
                            if (first_occur >= 0) {
                                // cut out 100 characters
                                var start = first_occur - 20;
                                var end = first_occur + 80;
                                if (start < 0) {
                                    start = 0;
                                }
                                if (start === 0) {
                                    end = 100;
                                }
                                if (end > content.length) {
                                    end = content.length;
                                }
                                var match_content = content.substr(start, end);
                                // highlight all keywords
                                keywords.forEach(function (keyword) {
                                    var regS = new RegExp(keyword, "gi");
                                    match_content = match_content.replace(regS, "<em class=\"search-keyword\">" + keyword + "</em>");
                                });

                                str += "<p class=\"search-result\">" + match_content + "...</p>"
                            }
                            str += "</li>";
                        }
                    });
                    str += "</ul>";
                    $resultContent.innerHTML = str;
                });
            }
        });
    };

    searchFunc('/search.xml', 'searchInput', 'searchResult');
});
</script>

    <!-- 白天和黑夜主题 -->
<div class="stars-con">
    <div id="stars"></div>
    <div id="stars2"></div>
    <div id="stars3"></div>  
</div>

<script>
    function switchNightMode() {
        $('<div class="Cuteen_DarkSky"><div class="Cuteen_DarkPlanet"></div></div>').appendTo($('body')),
        setTimeout(function () {
            $('body').hasClass('DarkMode') 
            ? ($('body').removeClass('DarkMode'), localStorage.setItem('isDark', '0'), $('#sum-moon-icon').removeClass("fa-sun").addClass('fa-moon')) 
            : ($('body').addClass('DarkMode'), localStorage.setItem('isDark', '1'), $('#sum-moon-icon').addClass("fa-sun").removeClass('fa-moon')),
            
            setTimeout(function () {
            $('.Cuteen_DarkSky').fadeOut(1e3, function () {
                $(this).remove()
            })
            }, 2e3)
        })
    }
</script>

    <!-- 回到顶部按钮 -->
<div id="backTop" class="top-scroll">
    <a class="btn-floating btn-large waves-effect waves-light" href="#!">
        <i class="fas fa-arrow-up"></i>
    </a>
</div>


    <script src="/libs/materialize/materialize.min.js"></script>
    <script src="/libs/masonry/masonry.pkgd.min.js"></script>
    <script src="/libs/aos/aos.js"></script>
    <script src="/libs/scrollprogress/scrollProgress.min.js"></script>
    <script src="/libs/lightGallery/js/lightgallery-all.min.js"></script>
    <script src="/js/matery.js"></script>

    

    
    
    

    <!-- 雪花特效 -->
    

    <!-- 鼠标星星特效 -->
    

     
        <script src="https://ssl.captcha.qq.com/TCaptcha.js"></script>
        <script src="/libs/others/TencentCaptcha.js"></script>
        <button id="TencentCaptcha" data-appid="xxxxxxxxxx" data-cbfn="callback" type="button" hidden></button>
    

    <!-- Baidu Analytics -->

    <!-- Baidu Push -->

<script>
    (function () {
        var bp = document.createElement('script');
        var curProtocol = window.location.protocol.split(':')[0];
        if (curProtocol === 'https') {
            bp.src = 'https://zz.bdstatic.com/linksubmit/push.js';
        } else {
            bp.src = 'http://push.zhanzhang.baidu.com/push.js';
        }
        var s = document.getElementsByTagName("script")[0];
        s.parentNode.insertBefore(bp, s);
    })();
</script>

    
    <script src="/libs/others/clicklove.js" async="async"></script>
    
    
    <script async src="/libs/others/busuanzi.pure.mini.js"></script>
    

    

    

    <!--腾讯兔小巢-->
    
    

    

    

    
    <script src="/libs/instantpage/instantpage.js" type="module"></script>
    

</body>

</html>
